做电子手抄报的网站,东莞哪里的网站建设效果好,wordpress写博客,去哪儿网站上做民宿需要材料【 声明#xff1a;版权所有#xff0c;欢迎转载#xff0c;请勿用于商业用途。 联系信箱#xff1a;feixiaoxing 163.com】 前面一篇文章#xff0c;我们学习了怎么创建qt的第一个工程#xff0c;怎么用designer给qt修改界面。虽然我们到目前为止#xff0c;还没有编写…【 声明版权所有欢迎转载请勿用于商业用途。 联系信箱feixiaoxing 163.com】 前面一篇文章我们学习了怎么创建qt的第一个工程怎么用designer给qt修改界面。虽然我们到目前为止还没有编写一行代码但是好像也做出了第一个qt软件即使这个软件好像没有什么实际用途。不过没关系我们今天可以进一步学习下怎么编写按钮的回调函数这样就可以让软件变得稍微有意义一点。 为了做到这一点我们开发了一个乘法计算的小软件。简单的来说软件有两个输入一个是数据1一个是数据2。通过一个calculate的按钮我们就可以得到数据1和数据2的乘积。另外我们还增加了一个clean按钮它可以对输入框进行清空的操作。 1、利用designer设计qt界面 如果对界面美观不做要求简单拖拉一下控件就可以实现这个目标。这部分内容之前一节已经讲解过不再赘述。 拖拉后控件的名字也可以通过ui文件看出来。ui文件就是普通的文本文件可以通过记事本或者notepad这样的软件打开就能找到里面的内容。相关名称在编写cpp代码的时候需要用到。 2、在QtWidgetsApplication1.h中添加函数声明 因为我们需要对两个按钮进行回调处理所以这边也需要在头文件的类当中添加两个回调函数。回调函数和其他普通函数稍微有点区别那就是添加一个额外的关键字slots这也算是qt的一个特色吧。
#pragma once#include QtWidgets/QMainWindow
#include ui_QtWidgetsApplication1.hclass QtWidgetsApplication1 : public QMainWindow
{Q_OBJECTpublic:QtWidgetsApplication1(QWidget *parent nullptr);~QtWidgetsApplication1();private:Ui::QtWidgetsApplication1Class ui;private slots:void on_calculate_clicked();void on_clean_clicked();
}; 3、在QtWidgetsApplication1.cpp实现回调函数 既然头文件当中已经声明了回调函数那么接下来就需要把控件和回调函数进行绑定。这样在按钮被按下去的时候才能触发对应的回调函数。目前这个bind的过程是利用connect函数完成的。另外对于ui里面的控件直接用ui.name的方法就可以直接找到对应的控件变量。
#include QApplication
#include QPushButton
#include QMessageBox
#include QtWidgetsApplication1.hQtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);connect(ui.pushButton, QPushButton::clicked, this, QtWidgetsApplication1::on_calculate_clicked);connect(ui.pushButton_2, QPushButton::clicked, this, QtWidgetsApplication1::on_clean_clicked);
}QtWidgetsApplication1::~QtWidgetsApplication1()
{} 注册好了接下来就可以一个一个实现对应的函数了。calculate是计算功能的按钮。按钮被按下去的时候首先会检查text1是否为空接着检查text2是否为空如果都ok就直接用QMessageBox弹出对应的计算结果。
void QtWidgetsApplication1::on_calculate_clicked() {QString text1;QString text2;text1 ui.lineEdit-text();if (text1 ){QMessageBox::information(this, tip, Please input data1!);return;}text2 ui.lineEdit_2-text();if (text2 ){QMessageBox::information(this, tip, Please input data2!);return;}int data1 text1.toInt();int data2 text2.toInt();data2 * data1;QMessageBox::information(this, tip, QString(Final result is ) QString::number(data2) QString(!));
} 结束了calculate按钮的回调下面就是clean按钮的回调。它的功能就简单得多主要就是把text1和text2中的内容清空就可以了。
void QtWidgetsApplication1::on_clean_clicked() {ui.lineEdit-setText();ui.lineEdit_2-setText();
} 4、测试和验证 和之前一个工程相比较这里除了ui和QtWidgetsApplication1文件修改之外不需要对main.cpp做任何操作。直接编译输入参数进行验证 有兴趣的同学还可以在回调函数的地方设置断点进一步进行分析测试锻炼一下自己的c调试能力。 文章转载自: http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.gpkjx.cn.gov.cn.gpkjx.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.nqbpz.cn.gov.cn.nqbpz.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn