使用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函数,用于连接事件和槽。下面是具体的三个函数。
后续:
现在回过头来看整个程序真是简单的不行,但是其实这么走过来还是有点不容易,因为c++里的东西一点都不会,所以在c程序转换到QT中时就费了很大劲,QT中字符串用的是Qstring,我得把本来程序里的字符数组转换成string,再转换成Qstring···算是体会到一些c语言落伍的感觉,跟这些东西不怎么搭··前期的准备也费了不少时间,主要是看Qt的教学视频,但是收获还是挺大的,后续可以试着添加一些别的功能。
原文:http://www.cnblogs.com/gongcr/p/5978936.html