嵌入式--QT]QThread 创建多线程代码示例
最编程
2024-07-10 18:04:38
...
QDiceThread.h
#ifndef QDICETHREAD_H
#define QDICETHREAD_H
#include <QObject>
#include <QThread>
#include <QMutex>
class QDiceThread : public QThread
{
Q_OBJECT
public:
QDiceThread();
~QDiceThread();
void run() Q_DECL_OVERRIDE;
void diceBegin();//掷一次骰子
void dicePause();//暂停
void stopThread();//结束线程
bool readValue(int* seq, int* diceValue);
signals:
void newValue(int seq, int diceValue);//产生新点数的信号
private :
int m_seq = 0;//掷骰子次数序号
int m_diceValue;//骰子点数
bool m_paused = true;//暂停
bool m_stop = false;//停止
QMutex mutex;
};
#endif // QDICETHREAD_H
QDiceThread.cpp
#include "QDiceThread.h"
#include <QTime>
#include <QMutexLocker>
QDiceThread::QDiceThread() {}
QDiceThread::~QDiceThread()
{
}
void QDiceThread::run()
{
m_stop = false;
m_seq = 0;
qsrand(QTime::currentTime().msec());
while(!m_stop) {
if(!m_paused) {
//QMutexLocker用法
// QMutexLocker Locker(&mutex);
//mutex用法
mutex.lock();
m_diceValue = qrand();
m_diceValue = (m_diceValue % 6)+1;
m_seq++;
//使用互斥量读取数据,暂时注释掉信号操作
// emit newValue(m_seq, m_diceValue);
//mutex用法
mutex.unlock();
}
msleep(500);
}
quit();
}
void QDiceThread::diceBegin()
{
m_paused = false;
}
void QDiceThread::dicePause()
{
m_paused = true;
}
void QDiceThread::stopThread()
{
m_stop = true;
}
bool QDiceThread::readValue(int* seq, int* diceValue)
{
if(mutex.tryLock()) {
*seq = m_seq;
*diceValue = m_diceValue;
mutex.unlock();
return true;
} else {
return false;
}
}
QThreadDialog.h
#ifndef QTHREADDIALOG_H
#define QTHREADDIALOG_H
#include <QDialog>
#include "QDiceThread.h"
#include <QTimer>
namespace Ui
{
class QThreadDialog;
}
class QThreadDialog : public QDialog
{
Q_OBJECT
public:
explicit QThreadDialog(QWidget* parent = nullptr);
~QThreadDialog();
void closeEvent(QCloseEvent* event);
private slots:
void on_pushButtonStart_clicked();
void on_pushButtonPause_clicked();
void on_pushButtonBegin_clicked();
void on_pushButtonStop_clicked();
void on_pushButtonClear_clicked();
public slots:
void threadStatusStart();
void threadStatusFinished();
void threadNewValue(int seq, int diceValue);
void onTimeOut();
private:
Ui::QThreadDialog* ui;
QDiceThread thread;
QTimer timer;
int mSeq, mDiceValue;
};
#endif // QTHREADDIALOG_H
QThreadDialog.cpp
#include "QThreadDialog.h"
#include "ui_QThreadDialog.h"
QThreadDialog::QThreadDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::QThreadDialog)
{
ui->setupUi(this);
connect(&thread, SIGNAL(started()), this, SLOT(threadStatusStart()));
connect(&thread, SIGNAL(finished()), this, SLOT(threadStatusFinished()));
connect(&thread, &QDiceThread::newValue, this, &QThreadDialog::threadNewValue);
connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
}
QThreadDialog::~QThreadDialog()
{
delete ui;
}
void QThreadDialog::closeEvent(QCloseEvent* event)
{
if(thread.isRunning()) {
thread.stopThread();
thread.wait();
}
event->accept();
}
void QThreadDialog::on_pushButtonStart_clicked()
{
thread.start();
mSeq = 0;
ui->pushButtonStart->setEnabled(false);
ui->pushButtonStop->setEnabled(true);
ui->pushButtonBegin->setEnabled(true);
ui->pushButtonPause->setEnabled(false);
}
void QThreadDialog::on_pushButtonStop_clicked()
{
thread.stopThread();
thread.wait();
ui->pushButtonStart->setEnabled(true);
ui->pushButtonStop->setEnabled(false);
ui->pushButtonBegin->setEnabled(false);
ui->pushButtonPause->setEnabled(false);
}
void QThreadDialog::on_pushButtonBegin_clicked()
{
thread.diceBegin();
timer.start(100);
ui->pushButtonBegin->setEnabled(false);
ui->pushButtonPause->setEnabled(true);
}
void QThreadDialog::on_pushButtonPause_clicked()
{
thread.dicePause();
timer.stop();
ui->pushButtonBegin->setEnabled(true);
ui->pushButtonPause->setEnabled(false);
}
void QThreadDialog::on_pushButtonClear_clicked()
{
ui->plainTextEdit->clear();
}
void QThreadDialog::threadStatusStart()
{
ui->labelStatus->setText("thread started");
}
void QThreadDialog::threadStatusFinished()
{
ui->labelStatus->setText("thread finished");
}
void QThreadDialog::threadNewValue(int seq, int diceValue)
{
QString str = QString::asprintf(u8"第%d次投掷骰子,点数为:%d", seq, diceValue);
ui->plainTextEdit->appendPlainText(str);
}
void QThreadDialog::onTimeOut()
{
int tmpSeq =0, tmpValue = 0;
bool valid = thread.readValue(&tmpSeq, &tmpValue);
if(valid && (tmpSeq != mSeq)) {
mSeq = tmpSeq;
mDiceValue = tmpValue;
QString str = QString::asprintf(u8"第%d次投掷骰子,点数为:%d", mSeq, mDiceValue);
ui->plainTextEdit->appendPlainText(str);
}
}
推荐阅读
-
嵌入式--QT]QThread 创建多线程代码示例
-
Qt 基本开发 Qt 多线程类 QThread 和 Qt 定时器类 QTimer 的详细方法和示例
-
.NET高级面试指南 Topic XVIII [ 介绍外观模式(Appearance Pattern),该模式提供了一个隐藏系统复杂性的简化界面 ]。- 简化复杂系统:当系统具有复杂的子系统结构时,可以使用外观模式来简化界面。提供统一界面:当客户端需要访问多个子系统时,可以使用外观模式提供统一界面。 外观模式在现代软件开发中得到广泛应用,尤其是在复杂系统中。例如 图形用户界面库:许多图形用户界面库(如 Qt、GTK+ 等)都使用外观模式来隐藏底层的复杂性,并为开发人员提供简单的界面来创建用户界面。 操作系统接口:操作系统中的系统调用和应用程序接口通常也使用外观模式来隐藏底层硬件和系统的复杂性,为应用程序提供访问系统资源的简单接口。企业应用程序:在可能涉及多个子系统的大型企业应用程序中,外观模式可用于封装这些子系统,并为客户端提供统一的使用界面。 网络框架:许多网络框架(如 ASP.NET MVC、Spring MVC 等)也使用外观模式来隐藏底层的复杂性,并为开发人员提供简单的接口来处理 HTTP 请求和响应。 集成开发环境(IDE):集成开发环境通常包含代码编辑器、编译器、调试器等多种功能。外观模式可用于封装这些功能,并为开发人员提供开发软件的简单界面。 代码示例: