发布时间:2025-12-11 02:28:22 浏览次数:1
具体内容如下
程序效果:
#ifndefDIALOG_H#defineDIALOG_H#include<QDialog>#include<QLabel>#include<QPushButton>#include<QTimer>#include<QStringList>classDialog:publicQDialog{Q_OBJECTpublic:Dialog(QWidget*parent=0);~Dialog();privateslots:voidon_clicked();voidon_timer();private:QLabel*label1;QPushButton*btn1;QTimer*tm;QStringListstrlist;};#endif//DIALOG_H#include"dialog.h"#include<QVBoxLayout>#include<QFont>#include<QFile>#include<QTextStream>#include<QMessageBox>Dialog::Dialog(QWidget*parent):QDialog(parent){setWindowTitle(tr("幸运大抽奖"));//设置窗口标题label1=newQLabel;label1->setText(tr("开始幸运大抽奖"));QFontfont;font.setBold(true);//设置字体为粗体font.setPointSize(80);//设置字号label1->setFont(font);btn1=newQPushButton;btn1->setText(tr("开始"));QVBoxLayout*layout1=newQVBoxLayout(this);layout1->addWidget(label1,0,Qt::AlignCenter);//加入label1标签,并且居中显示layout1->addWidget(btn1);//layout1->setSizeConstraint(QLayout::SetFixedSize);//设置layout大小和控件尺寸一致,使窗口不能更改大小QFilefile("student.txt");if(file.open(QFile::ReadOnly))//以只读的方式打开student.txt文件{QTextStreamstream(&file);while(!stream.atEnd()){strlist.append(stream.readLine());//将文件内容放到strlist中}file.close();}else{//如果打开student.txt文件失败,程序退出QMessageBox::critical(this,tr("错误"),file.errorString());exit(0);}tm=newQTimer(this);connect(tm,SIGNAL(timeout()),this,SLOT(on_timer()));connect(btn1,SIGNAL(clicked()),this,SLOT(on_clicked()));}Dialog::~Dialog(){}voidDialog::on_clicked(){staticboolstatus=true;if(status){btn1->setText("停止");//如果isok为true,设置按钮标题为“停止”tm->start(50);//启动计时器,没0.05秒执行一次on_timer函数status=false;}else{btn1->setText("开始");//如果isok为false,设置按钮标题为“开始”tm->stop();//停止计时器status=true;}}voidDialog::on_timer(){if(strlist.count()==0){return;//如果strlist中没有内容,函数返回}staticinti=0;label1->setText(strlist[i]);//从0到strlist.count(),循环显示strlist中每一项的内容i++;if(i>=strlist.count()){i=0;}}感谢各位的阅读!关于“C++如何实现幸运大抽奖”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!