当前位置: 首页 > news >正文

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

dialog.h

#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

#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
#include "mythread.h"namespace Ui {
class Dialog;
}class Dialog : public QDialog
{Q_OBJECTpublic: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

#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

#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

在这里插入图片描述

http://www.lryc.cn/news/222021.html

相关文章:

  • 基于厨师算法的无人机航迹规划-附代码
  • 设计模式的六大原则
  • 原文远知行COO张力加盟逐际动力 自动驾驶进入视觉时代?
  • 【公益案例展】火山引擎公益电子票据服务——连接善意,共创美好
  • postman中文乱码
  • 设计模式简要介绍
  • LeetCode-232. 用栈实现队列(C++)
  • 无人机红外相机的畸变矫正
  • C++编程案例讲解-基于结构体的控制台通讯录管理系统
  • ASP.NETCore6开启文件服务允许通过url访问附件(图片)
  • python爬取Web of science论文信息
  • 本地域名 127.0.0.1 / localhost
  • Python —— 不同类型的数据长度计算方式
  • NowCoder | 环形链表的约瑟夫问题
  • 华为政企数据中心网络交换机产品集
  • 多门店自助点餐+外卖二合一小程序源码系统 带完整搭建教程
  • kafka可视化工具
  • Excel 转 Json 、Node.js实现(应用场景:i18n国际化)
  • Redis7--基础篇2(Redis的十大数据类型及常用命令)
  • 1.HTML中网页介绍
  • 执行sql报错only_full_group_by的解决方法
  • 不学51直接学stm32可以吗?学stm32需要哪些基础?
  • 6.1二叉树的递归遍历(LC144,LC15,LC94)
  • Spring基础(3):复习
  • Java-Hbase介绍
  • 【PHP】【Too few arguments to function Firebase\JWT\JWT::encode()。。。。。。。】
  • Centos系统上安装包(软件)时常用的命令wget、rpm、yum分别是什么意思和作用?
  • 虹科干货 | 旧电脑别急着扔,手把手教你搭建NAS系统存储照片
  • python基础(Python高级特性(切片、列表生成式)、字符串的正则表达式、函数、模块、Python常用内置函数、错误处理)培训讲义
  • 计讯物联高精度GNSS接收机:担当小型水库大坝安全监测解决方案的“护航者”