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
相关推荐
江公望3 分钟前
QT/QML qmlRegisterType()函数浅谈
开发语言·qt
ZouZou老师3 小时前
Linux Qt出现xcb异常问题解决办法
开发语言·qt
雁门.13 小时前
qt封装dll及调用
开发语言·qt
办公自动化软件定制化开发python4 小时前
基于PyQt5开发的文件智能查找工具,开源思路+完整实现,解决办公文件检索痛点
开发语言·qt
深蓝海拓4 小时前
PySide6,QEventLoop.exec()的使用
笔记·python·qt·学习·pyqt
_OP_CHEN4 小时前
【从零开始的Qt开发指南】(二十)Qt 多线程深度实战指南:从基础 API 到线程安全,带你实现高效并发应用
开发语言·c++·qt·安全·线程·前端开发·线程安全
qq_401700415 小时前
CardLayout 实现自定义布局
qt
A星空1236 小时前
3519Hisidv500的QT配置
开发语言·qt
KeLin&6 小时前
讯为iTOP4412-Qt5.7环境搭建
开发语言·arm开发·qt·arm
Main. 246 小时前
从0到1学习Qt -- Qt3D入门
开发语言·qt·学习