QThread创建多线程程序
2024-10-08 16:01:59
最近在阅读Qt 5.9 C++开发指南,为了加深对书本上内容的理解,参照书上的讲解尝试写了一些demo,用于以后工作中查阅,如果涉及侵权请告知,实例程序samp13_1
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H #include <QThread> class MyThread : public QThread
{
Q_OBJECT private:
int _times;
int _value; protected:
void run(); public:
MyThread();
~MyThread(); signals:
void timesValueChanged(int times, int value); }; #endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QRandomGenerator> MyThread::MyThread()
{
_times = ;
_value = ;
} MyThread::~MyThread()
{ } // 这个是通过信号与槽与主窗口进行交互的,还有一种方法是通过函数
void MyThread::run()
{
while()
{
_value = QRandomGenerator::global()->generate(); // 用这个类生成随机数
_value = (_value % ) + ; // 这个类生成的数可能是负数,如果是负数就从新生成
if(_value <= )
{
continue;
} _times = _times + ;
emit timesValueChanged(_times, _value); // 发信号给主窗口,叫他显示 msleep(); // 先睡一会儿在生成数字
}
}
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H #include <mythread.h>
#include <QDialog>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QLabel> class MyDialog : public QDialog
{
Q_OBJECT private:
QPushButton *_btnStart;
QPushButton *_btnFinish;
QPushButton *_btnClearInfo;
QPlainTextEdit *_plainTextEdit;
QLabel *_labStatus;
QLabel *_labPicture;
MyThread *_thread; protected:
void closeEvent(QCloseEvent *event); public:
MyDialog(QWidget *parent = nullptr);
~MyDialog(); void iniUI();
void iniSignalSlots(); private slots:
void btnStartClicked();
void btnFinishClicked();
void btnClearInfoClicked();
void timesValueChanged(int times, int value); }; #endif // MYDIALOG_H
mydialog.cpp
#include "mydialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QPixmap>
#include <QString> MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
iniUI(); //创建线程
_thread = new MyThread(); iniSignalSlots();
} MyDialog::~MyDialog()
{ } // 初始化界面
void MyDialog::iniUI()
{
// 创建按钮
_btnStart = new QPushButton("启动游戏");
_btnFinish = new QPushButton("结束游戏");
_btnFinish->setEnabled(false);
_btnClearInfo = new QPushButton("清空文本"); // 布局按钮
QHBoxLayout *layout1 = new QHBoxLayout();
layout1->addWidget(_btnStart);
layout1->addWidget(_btnFinish);
layout1->addWidget(_btnClearInfo); // 创建文本框和图片显示框
_plainTextEdit = new QPlainTextEdit();
_labPicture = new QLabel();
QPixmap pixmap;
pixmap.load(":/images/d0.jpg");
_labPicture->setPixmap(pixmap); // 布局文本框和图片显示框
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addWidget(_plainTextEdit);
layout2->addWidget(_labPicture); // 创建状态栏
_labStatus = new QLabel("游戏状态:未开始"); // 布局状态栏
QHBoxLayout *layout3 = new QHBoxLayout();
layout3->addWidget(_labStatus); // 创建分组框的布局
QVBoxLayout *layout4 = new QVBoxLayout();
layout4->addLayout(layout1);
layout4->addLayout(layout2);
layout4->addLayout(layout3); // 创建一个分组框
QGroupBox *groupBox = new QGroupBox("掷骰子");
groupBox->setLayout(layout4); // 创建整体布局
QHBoxLayout *layout5 = new QHBoxLayout();
layout5->addWidget(groupBox); // 设置整体布局
setLayout(layout5);
resize(, );
} // 连接信号与槽
void MyDialog::iniSignalSlots()
{
connect(_btnStart, SIGNAL(clicked()), this, SLOT(btnStartClicked()));
connect(_btnFinish, SIGNAL(clicked()), this, SLOT(btnFinishClicked()));
connect(_btnClearInfo, SIGNAL(clicked()), this, SLOT(btnClearInfoClicked()));
connect(_thread, SIGNAL(timesValueChanged(int, int)), this, SLOT(timesValueChanged(int, int))); } void MyDialog::btnStartClicked()
{
_labStatus->setText("游戏状态:进行中");
_thread->start();
_btnStart->setEnabled(false);
_btnFinish->setEnabled(true);
} void MyDialog::btnFinishClicked()
{
_labStatus->setText("游戏状态:未开始");
_thread->terminate();
_thread->wait();
_btnStart->setEnabled(true);
_btnFinish->setEnabled(false);
} void MyDialog::btnClearInfoClicked()
{
_plainTextEdit->clear();
} // 当用户点击开始游戏后,线程就启动了,当线程生成一个数字时,会发一个信号过来,这个槽函数接受信号做出响应
void MyDialog::timesValueChanged(int times, int value)
{
QString text = QString::asprintf("第%d次掷骰子,点数为:%d", times, value);
_plainTextEdit->appendPlainText(text);
QString filename = QString::asprintf(":/images/d%d.jpg", value);
QPixmap pixmap;
pixmap.load(filename);
_labPicture->setPixmap(pixmap); // 给一个label设置图片
} void MyDialog::closeEvent(QCloseEvent *event)
{
if(_thread->isRunning()) // 如果线程还在继续执行,那么强行终止
{
_thread->terminate();
_thread->wait();
}
event->accept();
}
main.cpp
#include "mydialog.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog w;
w.show(); return a.exec();
}
效果展示
最新文章
- HTML5之文件API
- 另类的SQL注入方法
- .VDI manual Technical Logistics - Volume 2: Industrial Trucks
- B-tree&;B+tree
- redhat centos yum源的安装
- 模块化手机project ara之我见
- github fork, star and watch
- 大话PROFINET
- 【JavaScript的基本语法】
- Java并发框架——AQS中断的支持
- SpringCloud(6)---熔断降级理解、Hystrix实战
- jaeger 使用初探
- Linux命令归纳
- AI金融知识自学偏量化方向-了解不同类型的机器学习2
- ruby安装卸载
- nyoj576 集齐卡片赢大奖(一)
- 使用 Excel 可以很方便的做程序原型
- luvcview,使用mplayer查看摄像头和luvcview保存YUV图像视频的播放(转)
- 在js中对日期的加减法
- razor自定义函数 @helper 和@functions小结
热门文章
- jQuery序列化表单 serialize() serializeArray()(非常重要)
- 十二、js去掉空格_比较字符长度_中英文判断_页面初始化_简体字与繁字体判断
- 「Luogu1231」教辅的组成
- Python测试进阶——(2)配置PyCharm远程调试环境
- 7.8 Varnish Log
- ej3-1优先使用静态工厂方法而非构造函数来创建对象
- delphi的dbgrid控件点击title排序
- Django(九)模型:dj查询数据库的函数(方法)
- 吴裕雄--天生自然java开发常用类库学习笔记:集合工具类Collections
- Problem B: Bulbs