zoukankan      html  css  js  c++  java
  • 四则运算GUI设计2.0

    使用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.01)
    	{
    		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的教学视频,但是收获还是挺大的,后续可以试着添加一些别的功能。

  • 相关阅读:
    MSSQL Rebuild(重建)索引
    FileSystemWatcher触发多次Change事件的解决办法 .
    .NET和SQL Server中“空值”辨析 (DBNull与Null的区别)
    给 C# 开发者的代码审查清单
    sql server 中将由逗号“,”分割的一个字符串,转换为一个表,并应用与 in 条件
    远程MSMQ
    .net中的多线程
    C#使用Monitor类、Lock和Mutex类进行多线程同步
    如果是除去末尾特定字符或字符串:TrimEnd方法性能优于Remove方法
    C#编程总结(四)多线程应用
  • 原文地址:https://www.cnblogs.com/gongcr/p/5978936.html
Copyright © 2011-2022 走看看