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

dialog.h

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);// 更新标签中的文本
}

dialog.h

cpp 复制代码
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#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

mythread.cpp

cpp 复制代码
#include "mythread.h"
#include <QDebug>

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;
}

mythread.h

cpp 复制代码
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>

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
相关推荐
娇娇yyyyyy2 小时前
QT编程(15): Qt 按键事件和定时器事件
开发语言·qt
GIS阵地6 小时前
QgsDataSourceUri解析
数据库·c++·qt·开源软件·qgis
载数而行5207 小时前
Qt事件常用类,QLabel的QFrame交互
qt
娇娇yyyyyy9 小时前
QT编程(16): Qt Model
开发语言·qt
qq_4663024510 小时前
vs2022 与Qt版本兼容 带来的警告
c++·qt
娇娇yyyyyy1 天前
QT编程(13): Qt 事件机制eventfilter
开发语言·qt
带娃的IT创业者1 天前
工具状态失踪之谜:EventBus事件漏接与asyncio.Lock并发陷阱双线诊断
qt·websocket·并发控制·eventbus·事件驱动架构·pwa·asyncio.lock
不想看见4041 天前
C++/Qt 代码规范指南
开发语言·qt
li星野1 天前
QT模拟题:QT项目实践与架构设计(120分钟)
开发语言·qt
笑鸿的学习笔记1 天前
qt-C++语法笔记之Qt中的delete ui、ui的本质与Q_OBJECT
c++·笔记·qt