首页 >资讯参考 > > 正文

世界通讯!QThread run() 函数线程示例使用记录

发布日期:2023-05-11 07:03:46 来源:QT教程 分享


(相关资料图)

废话不多说 直接上代码

#ifndef QDICETHREAD_H#define QDICETHREAD_H#include #include #include class QDiceThread : public QThread{    Q_OBJECTpublic:    explicit QDiceThread(QObject *parent = nullptr);    void diceBegin();    void dicePause();    void stopThread();protected:    void run() Q_DECL_OVERRIDE;signals:    void newValue(int seq, int diceValue); //产生新点数的信号public slots:private:    int m_seq = 0;  //骰子次数序号    int m_diceValue; //骰子点数    bool m_pause = true; // 暂停    bool m_stop = false; //停止};#endif // QDICETHREAD_H#include \"qdicethread.h\"#include QDiceThread::QDiceThread(QObject *parent) : QThread(parent){}void QDiceThread::diceBegin(){    m_pause = false;}void QDiceThread::dicePause(){    m_pause = true;}void QDiceThread::stopThread(){    m_stop = true;}void QDiceThread::run(){    qDebug() << \"sdasdsd\";    qDebug() << QThread::currentThreadId();    m_stop = false;    m_seq =0;    qsrand(QTime::currentTime().msec()); //随机数初始化    while(!m_stop)    {        if(!m_pause)        {            m_diceValue =qrand();            m_diceValue = (m_diceValue %6)+1 ;            m_seq++;            emit newValue(m_seq,m_diceValue);        }        msleep(500);    }    quit(); //相当于exit(0),退出线程的时间循环}#ifndef DIALOG_H#define DIALOG_H#include #include \"qdicethread.h\"namespace Ui {class Dialog;}class Dialog : public QDialog{    Q_OBJECTprivate:    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#include \"dialog.h\"#include \"ui_dialog.h\"#include Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    ui->setupUi(this);    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.\");}void Dialog::onthreadA_finish(){    ui->label_2->setText(\"Thread状态:thread finish.\");}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(\":./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();}

【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】

点击这里:

标签:

Copyright ©  2015-2022 北方兽药网版权所有  备案号:京ICP备2021034106号-50   联系邮箱: 55 16 53 8@qq.com