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
相关推荐
疾风铸境8 小时前
qt+halcon开发相机拍照软件步骤
数码相机·qt·halcon·拍照
抠脚学代码9 小时前
Ubuntu Qt x64平台搭建 arm64 编译套件
数据库·qt·ubuntu
眠りたいです10 小时前
基于脚手架微服务的视频点播系统-播放控制部分
c++·qt·ui·微服务·云原生·架构·播放器
bikong715 小时前
一种高效绘制余晖波形的方法Qt/C++
数据库·c++·qt
追烽少年x16 小时前
QProxyStyle类中drawControl和drawComplexControl函数的区别是什么
qt
长沙红胖子Qt16 小时前
VTK开发笔记(五):示例Cone2,熟悉观察者模式,在Qt窗口中详解复现对应的Demo
qt·观察者模式·vtk·回调
郝学胜-神的一滴18 小时前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
华溢澄1 天前
macOS下基于Qt/C++的OpenGL开发环境的搭建
c++·qt·macos·opengl
刃神太酷啦1 天前
C++ 异常处理机制:从基础到实践的全面解析----《Hello C++ Wrold!》(20)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode
枫叶丹41 天前
【Qt开发】显示类控件(一)-> QLabel
开发语言·qt