基于vs&qt的线程

简单的线程的实列

掷骰子的线程实例
QDiceThread1.h

#pragma once

#include <QThread>

class QDiceThread : public QThread
{
	Q_OBJECT

public:
	QDiceThread();
	~QDiceThread();
	void diceBegin();//掷一次色子
	void dicePause();//暂停
	void stopThread();//结束线程
private:
	int m_seq = 0;//掷骰子次数序号
	int m_diceValue;//骰子点数
	bool m_Pauesed = true;//暂停
	bool m_stop = false;//停止
protected:
	void run() Q_DECL_OVERRIDE;//线程任务 重写

signals:
	void newValue(int seq, int diceValue);//产生新点数的信号

};

QDiceThread1.cpp

#include "QDiceThread1.h"
#include <QTime>
QDiceThread::QDiceThread()
{

}

QDiceThread::~QDiceThread()
{
}

void QDiceThread::diceBegin()//掷一次色子
{//开始掷色子
	m_Pauesed = false;
}
void QDiceThread::dicePause()//暂停
{//暂停掷色子
	m_Pauesed = true;
}
void QDiceThread::stopThread()//结束线程
{//停止线程
	m_stop = true;
}

void QDiceThread::run()//线程任务
{
	m_stop = false;//启动线程
	m_seq = 0;//次数
	qsrand(QTime::currentTime().msec());//随机数初始化

	while (!m_stop)
	{
		if (!m_Pauesed)
		{
			m_diceValue = qrand();
			m_diceValue = (m_diceValue % 6) + 1;
			m_seq++;
			emit newValue(m_seq, m_diceValue);
		}
		msleep(500);//线程休眠500ms
	}
	quit();//退出休眠
}

main.cpp

#include "QmainWindow.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
	QmainWindow w;
    w.show();
    return a.exec();
}

QmainWindow.h

#pragma once

#include <QMainWindow>
#include "QDiceThread1.h"
#include <QCloseEvent>
#include "ui_QmainWindow.h"

class QmainWindow : public QMainWindow
{
	Q_OBJECT

public:
	QmainWindow(QWidget *parent = Q_NULLPTR);
	~QmainWindow();
protected:
	void closeEvent(QCloseEvent *event);
private:
	Ui::QmainWindow ui;
	QDiceThread threadA;
private slots:
	void onthreadA_started();
	void onthreadA_finished();
	void onthreadA_newValue(int seq, int diceValue);
	void on_pushButtonStart_clicked();
	void on_pushButtonStop_clicked();
	void on_pushButtonBegin_clicked();
	void on_pushButtonDiceEnd_clicked();
	void on_pushButtonClear_clicked();
};


QmainWindow.cpp

#include "QmainWindow.h"
#include <QDebug>
#pragma execution_character_set("utf-8")
QmainWindow::QmainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	connect(&threadA, &QThread::start, this, &QmainWindow::onthreadA_started);
	connect(&threadA, &QThread::finished, this, &QmainWindow::onthreadA_started);
	connect(&threadA,&QDiceThread::newValue, this, &QmainWindow::onthreadA_newValue);
}

QmainWindow::~QmainWindow()
{
}

void QmainWindow::onthreadA_started()
{//线程打开start()信号的响应槽函数
	ui.label->setText("Thread状态:thread started");
}

void QmainWindow::onthreadA_finished()
{//线程的 finished()信号响应
	ui.label->setText("Thread状态:thread finished");
}

void QmainWindow::onthreadA_newValue(int seq, int diceValue)
{//newValue信号的响应函数,显示骰子次数和点数
	QString str = QString::asprintf("第 %d 次掷骰子,点数为 %d ", seq, diceValue);
	ui.textEdit->append(str);
	QPixmap pic;
	QString filename = QString::asprintf("D:/test_demo/qt/samp13_1Thread/images/%d.png", diceValue);
	qDebug() << filename;
	pic.load(filename);
	ui.labelPic->setPixmap(pic);

}

void QmainWindow::on_pushButtonStart_clicked()
{
	qDebug() << "on_pushButtonStart_clicked()";
	threadA.start();//开启线程
	ui.pushButtonStart->setEnabled(false);
	ui.pushButtonStop->setEnabled(true);
	ui.pushButtonBegin->setEnabled(true);
	ui.pushButtonDiceEnd->setEnabled(false);
	
}

void QmainWindow::on_pushButtonStop_clicked()
{
	qDebug() << "on_pushButtonStop_clicked()";
	threadA.stopThread();
	threadA.wait();
	ui.pushButtonStart->setEnabled(true);
	ui.pushButtonStop->setEnabled(false);
	ui.pushButtonBegin->setEnabled(false);
	ui.pushButtonDiceEnd->setEnabled(false);
}

void QmainWindow::on_pushButtonBegin_clicked()
{
	qDebug() << "on_pushButtonBegin_clicked()";
	threadA.diceBegin();
	ui.pushButtonBegin->setEnabled(false);
	ui.pushButtonDiceEnd->setEnabled(true);
}

void QmainWindow::on_pushButtonDiceEnd_clicked()
{
	qDebug() << "on_pushButtonDiceEnd_clicked()";
	threadA.dicePause();
	ui.pushButtonBegin->setEnabled(true);
	ui.pushButtonDiceEnd->setEnabled(false);
}

void QmainWindow::on_pushButtonClear_clicked()
{
	qDebug() << "on_pushButtonStart_clicked()";
	ui.textEdit->clear();
}


void QmainWindow::closeEvent(QCloseEvent *event)
{//窗口关闭事件 必须结束线程
	if (threadA.isRunning())
	{
		threadA.stopThread();
		threadA.wait();
	}
	qDebug() << "closeEvent(QCloseEvent *event)";
	event->accept();

}

这个是对工程的Ui设计和工程运行的结果图在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值