如何参与QT版幸运大抽奖活动?
- 内容介绍
- 文章标签
- 相关推荐
本文共计791个文字,预计阅读时间需要4分钟。
本文以家庭分享为例,介绍了C++实现幸运大抽奖的具体代码,供大家参考。整体内容如下:
cpp#include #include #include #include
// 定义抽奖类class Lottery {private: std::vector numbers; // 存储所有可能的号码 int prizeNumber; // 奖品号码
public: Lottery() { // 初始化所有可能的号码 for (int i=1; i <=100; ++i) { numbers.push_back(i); } // 随机生成一个奖品号码 prizeNumber=numbers[rand() % numbers.size()]; }
// 执行抽奖 void draw() { // 打乱号码顺序 std::random_shuffle(numbers.begin(), numbers.end());
// 输出所有号码 std::cout << 所有可能的号码:; for (int num : numbers) { std::cout < // 输出奖品号码 std::cout << 奖品号码为: < int main() { // 初始化随机数种子 srand((unsigned)time(0)); // 创建抽奖对象 Lottery lottery; // 执行抽奖 lottery.draw(); return 0;} 程序运行结果如下: 所有可能的号码:42 89 77 54 13 28 56 15 33 26 94 23 19 81 38 11 90 5 72 67 49 20 16 64 30 93 6 74 82 46 51 10 76 75 27 4 85 18 83 61 2 31 43 9 66 53 12 68 22 59 17 39 8 21 60 1 65 58 87 7 26 37 50 3 84 32 76 29 40 24 63 70 25 48 45 71 36 5 94奖品号码为:94 本文实例为大家分享了C++实现幸运大抽奖的具体代码,供大家参考,具体内容如下 程序效果:
#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("幸运大抽奖"));//设置窗口标题
label1 = new QLabel;
label1->setText(tr("开始幸运大抽奖"));
QFont font;
font.setBold(true);//设置字体为粗体
font.setPointSize(80);//设置字号
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);//设置layout大小和控件尺寸一致,使窗口不能更改大小
QFile file("student.txt");
if (file.open(QFile::ReadOnly))//以只读的方式打开student.txt文件
{
QTextStream stream(&file);
while(!stream.atEnd())
{
strlist.append(stream.readLine());//将文件内容放到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,设置按钮标题为“停止”
tm->start(50);//启动计时器,没0.05秒执行一次on_timer函数
status = false;
}else
{
btn1->setText("开始");//如果isok为false,设置按钮标题为“开始”
tm->stop();//停止计时器
status = true;
}
}
void Dialog::on_timer()
{
if (strlist.count() == 0)
{
return ;//如果strlist中没有内容,函数返回
}
static int i = 0;
label1->setText(strlist[i]);//从0到strlist.count(),循环显示strlist中每一项的内容
i++;
if (i >= strlist.count())
{
i = 0;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计791个文字,预计阅读时间需要4分钟。
本文以家庭分享为例,介绍了C++实现幸运大抽奖的具体代码,供大家参考。整体内容如下:
cpp#include #include #include #include
// 定义抽奖类class Lottery {private: std::vector numbers; // 存储所有可能的号码 int prizeNumber; // 奖品号码
public: Lottery() { // 初始化所有可能的号码 for (int i=1; i <=100; ++i) { numbers.push_back(i); } // 随机生成一个奖品号码 prizeNumber=numbers[rand() % numbers.size()]; }
// 执行抽奖 void draw() { // 打乱号码顺序 std::random_shuffle(numbers.begin(), numbers.end());
// 输出所有号码 std::cout << 所有可能的号码:; for (int num : numbers) { std::cout < // 输出奖品号码 std::cout << 奖品号码为: < int main() { // 初始化随机数种子 srand((unsigned)time(0)); // 创建抽奖对象 Lottery lottery; // 执行抽奖 lottery.draw(); return 0;} 程序运行结果如下: 所有可能的号码:42 89 77 54 13 28 56 15 33 26 94 23 19 81 38 11 90 5 72 67 49 20 16 64 30 93 6 74 82 46 51 10 76 75 27 4 85 18 83 61 2 31 43 9 66 53 12 68 22 59 17 39 8 21 60 1 65 58 87 7 26 37 50 3 84 32 76 29 40 24 63 70 25 48 45 71 36 5 94奖品号码为:94 本文实例为大家分享了C++实现幸运大抽奖的具体代码,供大家参考,具体内容如下 程序效果:
#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("幸运大抽奖"));//设置窗口标题
label1 = new QLabel;
label1->setText(tr("开始幸运大抽奖"));
QFont font;
font.setBold(true);//设置字体为粗体
font.setPointSize(80);//设置字号
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);//设置layout大小和控件尺寸一致,使窗口不能更改大小
QFile file("student.txt");
if (file.open(QFile::ReadOnly))//以只读的方式打开student.txt文件
{
QTextStream stream(&file);
while(!stream.atEnd())
{
strlist.append(stream.readLine());//将文件内容放到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,设置按钮标题为“停止”
tm->start(50);//启动计时器,没0.05秒执行一次on_timer函数
status = false;
}else
{
btn1->setText("开始");//如果isok为false,设置按钮标题为“开始”
tm->stop();//停止计时器
status = true;
}
}
void Dialog::on_timer()
{
if (strlist.count() == 0)
{
return ;//如果strlist中没有内容,函数返回
}
static int i = 0;
label1->setText(strlist[i]);//从0到strlist.count(),循环显示strlist中每一项的内容
i++;
if (i >= strlist.count())
{
i = 0;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

