QT线程之掷色子游戏
所有文件
dialog.h文件
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "qdicethread.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
private:
QDiceThread threadA;
protected:
void closeEvent(QCloseEvent *event);
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void onthreadA_start();
void onthreadA_finish();
void onThreadA_newValue(int seq,int diceValue);
void on_btnThreadBegin_clicked();
void on_btnThreadStop_clicked();
void on_btnDicestart_clicked();
void on_btnDiceStop_clicked();
void on_btnClear_clicked();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
qdicethread.h文件
#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
dialog.cpp文件
#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
if(threadA.isRunning())
{
qDebug("thread is running");
}
else
{
qDebug("thread is stopped");
}
connect(&threadA,SIGNAL(started()),this,SLOT(onthreadA_start()));
connect(&threadA,SIGNAL(finished()),this,SLOT(onthreadA_finish()));
connect(&threadA,SIGNAL(newValue(int,int)),this,SLOT(onThreadA_newValue(int,int)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::onthreadA_start()
{
ui->label_2->setText("Thread状态:thread start.");
if(threadA.isRunning())
{
qDebug("thread is running");
}
else
{
qDebug("thread is stopped");
}
}
void Dialog::onthreadA_finish()
{
ui->label_2->setText("Thread状态:thread finish.");
if(threadA.isRunning())
{
qDebug("thread is running");
}
else
{
qDebug("thread is stopped");
}
}
void Dialog::onThreadA_newValue(int seq, int diceValue)
{
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->label->setPixmap(pic);
}
void Dialog::on_btnThreadBegin_clicked()
{
threadA.start();
ui->btnThreadBegin->setEnabled(false);
ui->btnThreadStop->setEnabled(true);
ui->btnDiceStop->setEnabled(false);
ui->btnDicestart->setEnabled(true);
}
void Dialog::on_btnThreadStop_clicked()
{
threadA.stopThread();
threadA.wait();
ui->btnThreadBegin->setEnabled(true);
ui->btnThreadStop->setEnabled(false);
ui->btnDiceStop->setEnabled(false);
ui->btnDicestart->setEnabled(false);
}
void Dialog::on_btnDicestart_clicked()
{
threadA.diceBegin();
ui->btnDicestart->setEnabled(false);
ui->btnDiceStop->setEnabled(true);
}
void Dialog::on_btnDiceStop_clicked()
{
threadA.dicePause();
ui->btnDicestart->setEnabled(true);
ui->btnDiceStop->setEnabled(false);
}
void Dialog::on_btnClear_clicked()
{
ui->plainTextEdit->clear();
}
void Dialog::closeEvent(QCloseEvent *event)
{
if(threadA.isRunning())
{
threadA.stopThread();
threadA.wait();
}
event->accept();
}
qdicethread.cpp文件
#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_seq=0;
qsrand(QTime::currentTime().msec());
while(!m_stop)
{
if (!m_Paused)
{
m_seq++;
m_diceValue=qrand();
m_diceValue=m_diceValue%6+1;
emit newValue(m_seq,m_diceValue);
}
msleep(500);
}
quit();
}
dialog.ui
运行结果
还没有评论,来说两句吧...