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
相关推荐
xcyxiner15 小时前
DicomViewer (目录调整) 2
qt
xcyxiner17 小时前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能3 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G3 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G3 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G3 天前
71、打包发布---------打包发布
c++·qt
初圣魔门首席弟子3 天前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库
C++ 老炮儿的技术栈3 天前
Qt工控实战:自研机器人TCP长连接客户端(粘包处理+心跳保活+自动重连完整源码解析)
qt·tcp/ip·机器人
郝学胜-神的一滴3 天前
CMake 019:程序生成与清理全解析
开发语言·c++·qt·程序人生·软件构建·cmake
森G3 天前
76、仿ASIO实现的Linux c++服务器------服务器源码解析----云视频服务项目
c++·qt