C++實(shí)現(xiàn)幸運(yùn)大抽獎(jiǎng)(QT版)
更新時(shí)間:2022年06月14日 11:44:12 作者:吳英強(qiáng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)幸運(yùn)大抽獎(jiǎng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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("開(kāi)始幸運(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("開(kāi)始"));
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))//以只讀的方式打開(kāi)student.txt文件
{
QTextStream stream(&file);
while(!stream.atEnd())
{
strlist.append(stream.readLine());//將文件內(nèi)容放到strlist中
}
file.close();
}else
{
//如果打開(kāi)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í)器,沒(méi)0.05秒執(zhí)行一次on_timer函數(shù)
status = false;
}else
{
btn1->setText("開(kāi)始");//如果isok為false,設(shè)置按鈕標(biāo)題為“開(kāi)始”
tm->stop();//停止計(jì)時(shí)器
status = true;
}
}
void Dialog::on_timer()
{
if (strlist.count() == 0)
{
return ;//如果strlist中沒(méi)有內(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;
}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言浮點(diǎn)函數(shù)中的modf和fmod詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言浮點(diǎn)函數(shù)中的modf和fmod,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
詳解C++ STL vector容量(capacity)和大小(size)的區(qū)別
這篇文章主要介紹了詳解C++ STL vector容量(capacity)和大小(size)的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
模擬實(shí)現(xiàn)C語(yǔ)言中的內(nèi)存管理
這篇文章主要內(nèi)容是模擬C語(yǔ)言中的內(nèi)存管理,需要的朋友可以參考下2015-07-07
C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出,是數(shù)據(jù)結(jié)構(gòu)與算法中比較基礎(chǔ)的重要內(nèi)容,有必要加以牢固掌握,需要的朋友可以參考下2014-08-08

