C++實(shí)現(xiàn)幸運(yùn)大抽獎(jiǎng)(QT版)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)幸運(yùn)大抽獎(jiǎng)的具體代碼,供大家參考,具體內(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("幸運(yùn)大抽獎(jiǎng)"));//設(shè)置窗口標(biāo)題
 label1 = new QLabel;
 label1->setText(tr("開始幸運(yùn)大抽獎(jiǎng)"));
 QFont font;
 font.setBold(true);//設(shè)置字體為粗體
 font.setPointSize(80);//設(shè)置字號(hào)
 label1->setFont(font);
 
 btn1 = new QPushButton;
 btn1->setText(tr("開始"));
 
 QVBoxLayout *layout1 = new QVBoxLayout(this);
 layout1->addWidget(label1, 0, Qt::AlignCenter);//加入label1標(biāo)簽,并且居中顯示
 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("錯(cuò)誤"), 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è)置按鈕標(biāo)題為“停止”
  tm->start(50);//啟動(dòng)計(jì)時(shí)器,沒0.05秒執(zhí)行一次on_timer函數(shù)
  status = false;
 }else
 {
  btn1->setText("開始");//如果isok為false,設(shè)置按鈕標(biāo)題為“開始”
  tm->stop();//停止計(jì)時(shí)器
  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中每一項(xiàng)的內(nèi)容
 i++;
 if (i >= strlist.count())
 {
  i = 0;
 }
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C++實(shí)現(xiàn)病人就醫(yī)管理系統(tǒng)
欄 目:C語言
下一篇:OpenCV獲取鼠標(biāo)左鍵點(diǎn)擊位置圖像的像素值
本文標(biāo)題:C++實(shí)現(xiàn)幸運(yùn)大抽獎(jiǎng)(QT版)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/492.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
 - 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
 - 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
 - 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
 - 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
 - 01-10c++中inline的用法分析
 - 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
 - 01-10深入全排列算法及其實(shí)現(xiàn)方法
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 04-02jquery與jsp,用jquery
 - 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-10delphi制作wav文件的方法
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 


