国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++實現(xiàn)幸運大抽獎(QT版)

2020-01-26 13:33:25
字體:
供稿:網(wǎng)友

本文實例為大家分享了C++實現(xiàn)幸運大抽獎的具體代碼,供大家參考,具體內(nèi)容如下

程序效果:

#ifndef DIALOG_H#define DIALOG_H #include <QDialog>#include <QLabel>#include <QPushButton>#include <QTimer>#include <QStringList> class Dialog : public QDialog{ Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog();private slots: void on_clicked(); void on_timer();private: QLabel *label1; QPushButton *btn1; QTimer *tm; QStringList strlist;};  #endif // DIALOG_H
#include "dialog.h"#include <QVBoxLayout>#include <QFont>#include <QFile>#include <QTextStream>#include <QMessageBox> Dialog::Dialog(QWidget *parent) : QDialog(parent){ setWindowTitle(tr("幸運大抽獎"));//設(shè)置窗口標題 label1 = new QLabel; label1->setText(tr("開始幸運大抽獎")); QFont font; font.setBold(true);//設(shè)置字體為粗體 font.setPointSize(80);//設(shè)置字號 label1->setFont(font);  btn1 = new QPushButton; btn1->setText(tr("開始"));  QVBoxLayout *layout1 = new QVBoxLayout(this); layout1->addWidget(label1, 0, Qt::AlignCenter);//加入label1標簽,并且居中顯示 layout1->addWidget(btn1); // layout1->setSizeConstraint(QLayout::SetFixedSize);//設(shè)置layout大小和控件尺寸一致,使窗口不能更改大小  QFile file("student.txt"); if (file.open(QFile::ReadOnly))//以只讀的方式打開student.txt文件 {  QTextStream stream(&file);  while(!stream.atEnd())  {   strlist.append(stream.readLine());//將文件內(nèi)容放到strlist中  }  file.close(); }else {  //如果打開student.txt文件失敗,程序退出  QMessageBox::critical(this, tr("錯誤"), file.errorString());  exit(0); } tm = new QTimer(this); connect(tm, SIGNAL(timeout()), this, SLOT(on_timer())); connect(btn1, SIGNAL(clicked()), this, SLOT(on_clicked()));} Dialog::~Dialog(){ } void Dialog::on_clicked(){ static bool status = true; if (status) {  btn1->setText("停止");//如果isok為true,設(shè)置按鈕標題為“停止”  tm->start(50);//啟動計時器,沒0.05秒執(zhí)行一次on_timer函數(shù)  status = false; }else {  btn1->setText("開始");//如果isok為false,設(shè)置按鈕標題為“開始”  tm->stop();//停止計時器  status = true; }} void Dialog::on_timer(){ if (strlist.count() == 0) {  return ;//如果strlist中沒有內(nèi)容,函數(shù)返回 }  static int i = 0; label1->setText(strlist[i]);//從0到strlist.count(),循環(huán)顯示strlist中每一項的內(nèi)容 i++; if (i >= strlist.count()) {  i = 0; }}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沈阳市| 贵南县| 旬邑县| 潮安县| 武威市| 榆林市| 城市| 尉犁县| 内乡县| 治县。| 思南县| 德惠市| 石嘴山市| 高碑店市| 措勤县| 湄潭县| 嘉定区| 南投市| 遂溪县| 旬阳县| 石楼县| 浦城县| 略阳县| 道真| 乌拉特中旗| 土默特左旗| 永兴县| 朝阳市| 邵阳市| 仁寿县| 宁乡县| 洪雅县| 安图县| 乌鲁木齐县| 都兰县| 恩平市| 安岳县| 察雅县| 澄江县| 塔城市| 策勒县|