0. 程序介绍:
这是Qt5.9教案中的骰子多线程第一个例子
1.运行结果
2.工程文件
#-------------------------------------------------
#
# Project created by QtCreator 2023-04-12T18:49:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = DiceThread
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
dialog.cpp \
qdicethread.cpp
HEADERS += \
dialog.h \
qdicethread.h
FORMS += \
dialog.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc
3. 子线程类
头文件
#ifndef QDICETHREAD_H
#define QDICETHREAD_H
#include <QThread>
class QDiceThread : public QThread
{
Q_OBJECT
private:
int m_seq=0;
int m_diceValue;
bool m_Paused=true;
bool m_stop = false;
protected:
void run() Q_DECL_OVERRIDE;
public:
QDiceThread();
void diceBegin();
void dicePause();
void stopThread();
signals:
void newValue(int seq, int diceValue);
};
#endif // QDICETHREAD_H
实现文件
#include "qdicethread.h"
#include <QTime>
QDiceThread::QDiceThread()
{
}
void QDiceThread::diceBegin()
{ //开始掷骰子
m_Paused=false;
}
void QDiceThread::dicePause()
{//暂停掷骰子
m_Paused=true;
}
void QDiceThread::stopThread()
{//停止线程
m_stop=true;
}
void QDiceThread::run()
{//线程任务
m_stop=false;//启动线程时令m_stop=false
m_seq=0; //掷骰子次数
qsrand(QTime::currentTime().msec());//随机数初始化,qsrand是线程安全的
while(!m_stop)//循环主体
{
if (!m_Paused)
{
m_diceValue=qrand(); //获取随机数
m_diceValue=(m_diceValue % 6)+1;
m_seq++;
emit newValue(m_seq,m_diceValue); //发射信号
}
msleep(500); //线程休眠500ms
}
quit();//相当于 exit(0),退出线程的事件循环
}
4.界面程序实现
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "qdicethread.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
protected:
void closeEvent(QCloseEvent *event);
private slots:
void on_btnStartThread_clicked();
void on_btnDiceBegin_clicked();
void on_btnDiceEnd_clicked();
void on_btnStopThread_clicked();
void on_btnClear_clicked();
void onthreadDice_started();
void onthreadDice_finished();
void onthreadDice_newValue(int seq, int diceValue);
private:
Ui::Dialog *ui;
QDiceThread threadDice;
};
#endif // DIALOG_H
#pragma execution_character_set("utf-8")
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(&threadDice,SIGNAL(started()),this,SLOT(onthreadDice_started()));
connect(&threadDice,SIGNAL(finished()),this,SLOT(onthreadDice_finished()));
connect(&threadDice,SIGNAL(newValue(int,int)),this,SLOT(onthreadDice_newValue(int,int)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_btnStartThread_clicked()
{
threadDice.start();
ui->btnStartThread->setEnabled(false);
ui->btnStopThread->setEnabled(true);
ui->btnDiceBegin->setEnabled(true);
ui->btnDiceEnd->setEnabled(false);
}
void Dialog::on_btnDiceBegin_clicked()
{
threadDice.diceBegin();
ui->btnDiceBegin->setEnabled(false);
ui->btnDiceEnd->setEnabled(true);
}
void Dialog::on_btnDiceEnd_clicked()
{
threadDice.dicePause();
ui->btnDiceBegin->setEnabled(true);
ui->btnDiceEnd->setEnabled(false);
}
void Dialog::on_btnStopThread_clicked()
{
threadDice.stopThread();
threadDice.wait();
ui->btnStartThread->setEnabled(true);
ui->btnStopThread->setEnabled(false);
ui->btnDiceBegin->setEnabled(false);
ui->btnDiceEnd->setEnabled(false);
}
void Dialog::on_btnClear_clicked()
{
ui->plainTextEdit->clear();
}
void Dialog::closeEvent(QCloseEvent *event)
{
if (threadDice.isRunning())
{
threadDice.stopThread();
threadDice.wait();
}
event->accept();
}
void Dialog::onthreadDice_started()
{//线程的started()信号的响应槽函数
ui->LabA->setText("Thread状态:thread started");
}
void Dialog::onthreadDice_finished()
{//线程的 finished()信号的响应槽函数
ui->LabA->setText("Thread状态:thread finished");
}
void Dialog::onthreadDice_newValue(int seq,int diceValue)
{//QDiceThread的newValue()信号的响应槽函数,显示骰子次数和点数
QString str=QString::asprintf("第 %d 次掷骰子,点数为:%d",seq,diceValue);
ui->plainTextEdit->appendPlainText(str);
QPixmap pic; //图片显示
QString filename=QString::asprintf(":/dice/images/d%d.jpg",diceValue);
pic.load(filename);
ui->LabPic->setPixmap(pic);
}
5、主程序
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}