#include "widget.h"
#include <QObject>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(600, 300);
this->setFixedSize(600, 300);
this->setWindowTitle("ASCII CONVERT");
In_Lable.setParent(this); //放进窗口
In_Lable.resize(200,50); //设置按钮大小
In_Lable.setText("请输入Dec");
In_Lable.move(0,0); //移动按钮
Out_Dec_Lable.setParent(this); //放进窗口
Out_Dec_Lable.resize(200,50); //设置按钮大小
Out_Dec_Lable.setText("字符");
Out_Dec_Lable.move(200,0); //移动按钮
Out_Hex_Lable.setParent(this); //放进窗口
Out_Hex_Lable.resize(200,50); //设置按钮大小
Out_Hex_Lable.setText("Hex");
Out_Hex_Lable.move(400,0); //移动按钮
In_Edit.setParent(this); //放进窗口
In_Edit.resize(200,200); //设置按钮大小
In_Edit.move(0,50); //移动按钮
Out_Char_Edit.setParent(this); //放进窗口
Out_Char_Edit.resize(200,200); //设置按钮大小
Out_Char_Edit.move(200,50); //移动按钮
Out_Hex_Edit.setParent(this); //放进窗口
Out_Hex_Edit.resize(200,200); //设置按钮大小
Out_Hex_Edit.move(400,50); //移动按钮
Get_Button.setParent(this); //放进窗口
Get_Button.setText("确定"); //设置按钮文字
Get_Button.resize(200,50); //设置按钮大小
Get_Button.move(0,250); //移动按钮
Example_Button.setParent(this); //放进窗口
Example_Button.setText("例子"); //设置按钮文字
Example_Button.resize(200,50); //设置按钮大小
Example_Button.move(200,250); //移动按钮
Clear_Button.setParent(this); //放进窗口
Clear_Button.setText("清除"); //设置按钮文字
Clear_Button.resize(200,50); //设置按钮大小
Clear_Button.move(400,250); //移动按钮
connect(&Get_Button, &QPushButton::clicked, this, &Widget::Get_Button_clicked);
connect(&Example_Button, &QPushButton::clicked, this, &Widget::Example_Button_clicked);
connect(&Clear_Button, &QPushButton::clicked, this, &Widget::Clear_clicked);
connect(&In_Edit, &QTextEdit::textChanged, this, &Widget::In_Edit_clicked);
}
Widget::~Widget()
{
}
Widget::In_Edit_clicked()
{
//只能输入数字
QRegExp reg;
reg.setPattern("^[0-9,]+$");
QString strText = In_Edit.toPlainText();
if (reg.exactMatch(strText))
{
m_symtext = strText;
}
else
{
if(strText.length() > 0)
{
int offset = strText.length() - m_symtext.length();
//改变光标的位置
QTextCursor cursor = In_Edit.textCursor();
auto pos = cursor.position();
pos = pos >= offset ? pos - offset : pos;
In_Edit.setText(m_symtext);
cursor.setPosition(pos);
In_Edit.setTextCursor(cursor);
}
}
return 0;
}
Widget::Example_Button_clicked()
{
disconnect(&In_Edit, &QTextEdit::textChanged, this, &Widget::In_Edit_clicked);
Clear_clicked();
In_Edit.append("49");
Get_Button_clicked();
connect(&In_Edit, &QTextEdit::textChanged, this, &Widget::In_Edit_clicked);
return 0;
}
Widget::Get_Button_clicked()
{
QString input_string = In_Edit.toPlainText();
uint32_t buff = 0;
if(input_string.length() > 0)
{
buff = input_string.toInt(0, 10);
if(buff > 127)
{
Out_Char_Edit.setText("输入的值不在ASCII范围");
}
else
{
Out_Hex_Edit.setText(QString("0x%1").arg(buff, 0, 16));
Out_Char_Edit.setText((QString)buff);
}
}
else
{
In_Edit.setText("请输入字符");
}
return 0;
}
Widget::Clear_clicked()
{
In_Edit.clear();
Out_Hex_Edit.clear();
Out_Char_Edit.clear();
return 0;
}