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
相关推荐
茉莉玫瑰花茶2 小时前
Qt 界面优化 --- 绘图
开发语言·数据库·qt
掘根2 小时前
【Qt】容器类控件——QTabWidget
开发语言·qt
hqwest2 小时前
QT肝8天07--连接数据库
开发语言·数据库·c++·qt·sqlite·上位机·qt开发
海涛高软13 小时前
qt使用opencv的imread读取图像为空
qt·opencv·webpack
jjjxxxhhh12316 小时前
【QT】-QT为啥喜欢用类而不是结构体
开发语言·qt
小-黯19 小时前
VSCode+QT开发环境配置
ide·vscode·qt
AI+程序员在路上21 小时前
QT6中QGraphicsView功能与应用
开发语言·c++·qt
郝学胜-神的一滴1 天前
深入理解 Qt 元对象系统:QMetaEnum 的应用与实践
开发语言·c++·qt·软件工程
ao_lang1 天前
Qt事件处理全解析
开发语言·qt
weixin_417257061 天前
Qt解决不同线程,调用对方的函数
开发语言·qt