石柱网站制作,网站设计制作价钱,快速搭建网站软件,彩票娱乐网站建设【 声明#xff1a;版权所有#xff0c;欢迎转载#xff0c;请勿用于商业用途。 联系信箱#xff1a;feixiaoxing 163.com】 用抽奖软件抽奖#xff0c;是一种很常见的抽奖方式。特别是写这篇文章的时候#xff0c;正好处于2023年12月31日#xff0c;也是一年中最后一天…【 声明版权所有欢迎转载请勿用于商业用途。 联系信箱feixiaoxing 163.com】 用抽奖软件抽奖是一种很常见的抽奖方式。特别是写这篇文章的时候正好处于2023年12月31日也是一年中最后一天。虽然今年过年晚一点但是2到3个星期之后基本上各家公司都会准备年终尾牙的活动了。年会上吃饭、表演节目这是标配不过重头戏还是抽奖因为它会极大地调动员工的情绪也会让员工更有归属感。 最近我们学习了QT程序正好可以用QT设计一个简单的抽奖程序。 1、利用qt designer进行界面设计 界面设计比较简单主要就是3个label和2个按钮。label的作用就是显示最终的中奖号码。2个按钮一个是启动一个是结束。启动按钮按下去的时候3个数字开始跳动结束按钮按下去的时候数字停止跳动。最终留下来的数字提示我们幸运儿就是这位朋友。 2、QtWidgetsApplication.h头文件 代码实现部分呢有点类似于前面两篇文章的结合同时增加一个随机数生成的功能。首先两个按钮肯定需要绑定必要的按钮回调函数。其次按钮按下去的时候会触发定时器操作这又相当于之前倒计时软件的设计。最后因为每次需要显示的数据不同所以还需要增加一个随机数生成的功能。要实现这些功能我们看下头文件怎么设计
#pragma once#include QtWidgets/QMainWindow
#include QTimer
#include ui_QtWidgetsApplication.hclass QtWidgetsApplication : public QMainWindow
{Q_OBJECTpublic:QtWidgetsApplication(QWidget *parent nullptr);~QtWidgetsApplication();private:Ui::QtWidgetsApplicationClass ui;QTimer* p_timer;int num;int flag;void display_number();private slots:void update();void on_start_clicked();void on_stop_clicked();
};3、QtWidgetsApplication.cpp文件编写 要实现前面需要的功能还是要一步一步来开发。通常我们先处理下构造函数和析构函数随后处理按钮回调函数最后处理定时器函数的部分。
#include QRandomGenerator#include QtWidgetsApplication.hQtWidgetsApplication::QtWidgetsApplication(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);//set font and colorQFont ft;ft.setPointSize(20);ui.label1-setFont(ft);ui.label2-setFont(ft);ui.label3-setFont(ft);ui.label1-setStyleSheet(color: blue;);ui.label2-setStyleSheet(color: blue;);ui.label3-setStyleSheet(color: blue;);#if 0 // set title in ui file by feixiaoxingproperty name windowTitlestringLotteryDemo / string / property
#endif// initialize variablenum 0;flag 0;p_timer NULL;// connect button with functionconnect(ui.pushButton1, QPushButton::clicked, this, QtWidgetsApplication::on_start_clicked);connect(ui.pushButton2, QPushButton::clicked, this, QtWidgetsApplication::on_stop_clicked);ui.pushButton1-setEnabled(true);ui.pushButton2-setEnabled(false);// display number heredisplay_number();
}// destructor function
QtWidgetsApplication::~QtWidgetsApplication()
{if (p_timer) delete p_timer;
}// shou data here
void QtWidgetsApplication::display_number()
{ui.label1-setText(QString::number(num / 100));ui.label2-setText(QString::number((num % 100) / 10));ui.label3-setText(QString::number(num % 10));
} 类的构造函数和析构函数非常重要很多资源的申请和释放都是放在这里。当然这里不仅包括了上面说的两个函数还有一个display_number函数。大部分内容都是之前文章已经讨论过的这里不再赘述。唯一需要补充的就是窗口标题的修改。目前为止个人认为比较好的办法还是直接修改ui文件。
// start button callback function
void QtWidgetsApplication::on_start_clicked()
{// release previous timerif (p_timer){delete p_timer;p_timer NULL;}// create a num, 0~999num QRandomGenerator::global()-bounded(1000);// create timerp_timer new QTimer(this);connect(p_timer, SIGNAL(timeout()), this, SLOT(update()));p_timer-start(20); // 20 is intervaldisplay_number();// set buttonui.pushButton1-setEnabled(false);ui.pushButton2-setEnabled(true);
}// timeout callback function
void QtWidgetsApplication::update()
{if (flag){p_timer-stop();delete p_timer;p_timer NULL;flag 0;return;}num QRandomGenerator::global()-bounded(1000);display_number();
} 处理完了构造函数、析构函数剩下来就是按钮的处理。start按钮的回调已经在构造函数中注册好了当然stop也是一样。我们只需要实现具体的内容即可。因为是抽奖它和倒计时不一样所以定时器的创建和回调需要在按钮触发的时候才能添加这和之前不太一样。当然函数中涉及到了随机数引用QRandomGenerator处理一下即可。
// stop button callback function
void QtWidgetsApplication::on_stop_clicked()
{flag 1;ui.pushButton1-setEnabled(true);ui.pushButton2-setEnabled(false);
}有开始就有结束。这里结束的处理方式就是flag置位同时灰化一个按钮高亮一个按钮。前面定时器回调的时候也用到了这个flag置位即如果发现flag为1立即停止定时器、删除定时器。 4、测试和验证 代码本身不复杂直接拿过来编译和测试即可。编译无误的话首先单击start数字是否开始跳动继续单击stop数字是否不再跳动。如此操作几次没有发现问题的话就说明我们开发的代码是ok的不然就要回去检查一下原因了看看问题发生在什么地方。 文章转载自: http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.bxqtq.cn.gov.cn.bxqtq.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn