• qt多线程例子,不断输出数字


    dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    // 启动线程按钮
    void Dialog::on_startButton_clicked()
    {
    
        //connect(&thread, SIGNAL(showNum(int)), this, SLOT(updateLabel(int))); //方法一
        //connect(&thread, &MyThread::showNum, this, &Dialog::updateLabel); //方法二
        //QObject::connect(发送者, &发送者类::信号, 接收者, &接收者类::槽函数);
        QObject::connect(&thread, &MyThread::showNum, this, [this](int number) { //方法三
            QString text = "in MyThread:" + QString::number(number);// 将数字转换为字符串
            ui->label->setText(text);
        });
        thread.start();
        ui->startButton->setEnabled(false);
        ui->stopButton->setEnabled(true);
    }
    
    // 终止线程按钮
    void Dialog::on_stopButton_clicked()
    {
        if (thread.isRunning()) {
            thread.stop();
            ui->startButton->setEnabled(true);
            ui->stopButton->setEnabled(false);
        }
    }
    
    void Dialog::closeEvent(QCloseEvent *event)
    {
        if (thread.isRunning()){
            thread.terminate();
            thread.wait();
        }
    }
    
    void Dialog::updateLabel(int number)
    {
        QString text = QString::number(number);// 将数字转换为字符串
        ui->label->setText(text);// 更新标签中的文本
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include 
    #include "mythread.h"
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = nullptr);
        ~Dialog();
    
    private slots:
        void on_startButton_clicked();
    
        void on_stopButton_clicked();
    
        void updateLabel(int number);
    
    private:
        Ui::Dialog *ui;
        MyThread thread;
    
        // QWidget interface
    protected:
        virtual void closeEvent(QCloseEvent *event) override;
    };
    
    
    
    #endif // DIALOG_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    mythread.cpp

    #include "mythread.h"
    #include 
    
    MyThread::MyThread(QObject *parent) :
        QThread(parent)
    {
        stopped = false;
    }
    
    void MyThread::run()
    {
        qreal i = 0;
        while (!stopped) {
            qDebug() << QString("in MyThread: %1").arg(i);
            emit showNum(i);
            msleep(1000);
            i++;
        }
        stopped = false;
    }
    
    void MyThread::stop()
    {
        stopped = true;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    mythread.h

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    
    #include 
    
    class MyThread : public QThread
    {
        Q_OBJECT
    public:
        explicit MyThread(QObject *parent = nullptr);
        void stop();
    protected:
        void run() override;
    private:
        volatile bool stopped;
    
    signals:
        void showNum(int num);
    };
    
    
    #endif // MYTHREAD_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

  • 相关阅读:
    (笔记整理未完成)【字符串算法:Trie树】
    MyBatis
    Java架构师之路八、安全技术:Web安全、网络安全、系统安全、数据安全等
    Hive【Hive(五)函数-高级聚合函数、炸裂函数】
    二叉树的几个递归问题
    实体类属性名与数据库列名不一致解决方案
    聚观早报|苹果明年iPhone基带继续由高通提供
    【数据结构Java版】LinkedList与链表
    遵循开源软件安全路线图
    LVGL学习(5):物理按键切换焦点之焦点保存和恢复
  • 原文地址:https://blog.csdn.net/qq_34974229/article/details/134266675