标签:
使用QT设计的界面如下:
程序流程是点击开始出题,会在题目后面的框中显示所出的题目,在输入答案以后点击提交答案会判断输入的答案是否正确。
输入后的界面:
部分代码如下:
qtyunsuan.h文件:
class Qtyunsuan : public QMainWindow { Q_OBJECT public: Qtyunsuan(QWidget *parent = 0); ~Qtyunsuan(); private slots: int OnShowQue(); private slots: int OnGetAns(); private slots: int OnReturnPressed(); private: QString qss; Ui::QtyunsuanClass ui; };
主要是定义了三个槽,用于与相应的事件连接。OnShowQue()用来显示题目;OnGetAns()用来获取输入的答案并且判断对错;OnReturnPressed()是输入答案后不点击提交答案按钮而是按回车键也可以处理输入的答案;
qtyunsuan.cpp文件:
Qtyunsuan::Qtyunsuan(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); connect(ui.Btnstart, SIGNAL(clicked()), this, SLOT(OnShowQue()));//开始出题按钮按下,显示一道题目 connect(ui.Btnover, SIGNAL(clicked()), this, SLOT(OnGetAns()));//提交答案按钮按下,获取输入的答案,显示是否正确 connect(ui.Editanswer, SIGNAL(returnPressed()), this, SLOT(OnReturnPressed()));//输入答案后按回车键,显示是否正确 } Qtyunsuan::~Qtyunsuan() { } int Qtyunsuan::OnShowQue() { qss = abc(); ui.Editquestion->setText(qss); ui.Editanswer->setText(""); return 0; } int Qtyunsuan::OnGetAns() { QString str1=ui.Editanswer->text();//获取用户输入的答案 string ans; ans = str1.toStdString();//将用户输入的Qstring类型答案转换成string类型 double answer= stringToNum<double>(ans);//将答案由string类型转换成double double answer1 = Calculate(expression); if (abs(answer1 - answer) < 0.1) { QMessageBox::information(this, "Right", "You are right"); } else { QMessageBox::information(this, "Wrong", "You are wrong"); } return 0; } int Qtyunsuan::OnReturnPressed() { OnGetAns(); return 0; }
上面是三个connect函数,用于连接事件和槽。下面是具体的三个函数。
标签:
原文地址:http://www.cnblogs.com/brilliant2016/p/5983080.html