QT多线程之---moveToThread用法

news/2024/7/16 6:06:59

  在gui编程里,一个子函数的运行时间可能过长,界面就处于假死状态,原因是窗口是一个线程,子函数也在这个线程里,一些事件也要在这个线程里处理。

如果子函数运行时间过长,系统没有办法调用事件监听循环,gui就处于假死。一般有两种办法:

      子函数事件不是很长,可以在子函数中间插入一些 QCoreApplication::processEvents 

       另一种方法就是把耗时的工作放到另一个线程里,通过信号槽来传递。这里介绍Qobject的moveToThread方法。

       下面使用老板和员工的例子来讲,有两个BT老板,闲的蛋疼,安了个闹钟,每个一段时间查员工的岗。

代码如下:

  

#include <QCoreApplication>
#include <QThread>
#include <QTimer>
#include <QObject>
#include <QDebug>
#include "worker.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<<"main thread is :"<<QThread::currentThreadId()<<endl;    //打印主线程的线程号
   
QTimer *boss1 = new QTimer(); boss1->setInterval(5000); QThread *t1=new QThread(); //来一个新的线程 t1->start(); //启动线程 Worker w1("Bob"); //创建一个对象 w1.moveToThread(t1); //把这个对象移到线程t1里 QObject::connect(boss1, SIGNAL(timeout()), &w1, SLOT(run())); //通过信号槽机制将boss1和worker1连接起来,每个一段时间查一次看看员工工作没 QTimer *boss2 =new QTimer();
boss2->setInterval(3000); QThread *t2=new QThread(); //来一个新的线程 t2->start(); //启动线程 Worker w2("Stuart"); //创建一个对象 w2.moveToThread(t2); //把这个对象移到线程t2里 QObject::connect(boss2, SIGNAL(timeout()), &w2, SLOT(run())); boss1->start(); boss2->start(); return a.exec(); }

 worker类

  注意的是worker类一定要继承Qobject

#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <iostream>
class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QString name, QObject *parent = nullptr);
    long runnum=0;
    QString name;

signals:

public slots:
    void run(void);
};

#endif // WORKER_H

 

#include "worker.h"
#include <QDebug>
#include <QThread>
Worker::Worker(QString name ,QObject *parent) : name(name),QObject(parent)
{
    qDebug()<<"Hi, I'm worker:"<<name<<" at thread number:"<<QThread::currentThreadId()<<endl;
}

void Worker::run()
{
    runnum++;
    qDebug()<<"I'm "<<name<<" don't spy me so frequently boss! I'm busy in my work..........";
    qDebug()<<"you have spy me "<<runnum<<" times!"<< "I'm working at thread num :"<<QThread::currentThreadId()<<endl;
    long s=1000;
    for (long i=0; i<s;i++){


    }

}

 运行后的结果:

main thread is : 0x6be0

Hi, I'm worker: "Bob"  at thread number: 0x6be0

Hi, I'm worker: "Stuart"  at thread number: 0x6be0

I'm  "Stuart"  don't spy me so frequently boss! I'm busy in my work..........
you have spy me  1  times! I'm working at thread num : 0x36c0

I'm  "Bob"  don't spy me so frequently boss! I'm busy in my work..........
you have spy me  1  times! I'm working at thread num : 0x5ff0

I'm  "Stuart"  don't spy me so frequently boss! I'm busy in my work..........
you have spy me  2  times! I'm working at thread num : 0x36c0

I'm  "Stuart"  don't spy me so frequently boss! I'm busy in my work..........
you have spy me  3  times! I'm working at thread num : 0x36c0

I'm  "Bob"  don't spy me so frequently boss! I'm busy in my work..........
I'm  "Stuart"  don't spy me so frequently boss! I'm busy in my work..........
you have spy me  2  times! I'm working at thread num : 0x5ff0

you have spy me  4  times! I'm working at thread num : 0x36c0

I'm  "Bob"  don't spy me so frequently boss! I'm busy in my work..........
I'm  "Stuart"  don't spy me so frequently boss! I'm busy in my work..........

 

不知道你们又遇到BT的老板吗。

 

转载于:https://www.cnblogs.com/yjphhw/p/10948962.html


http://www.niftyadmin.cn/n/712297.html

相关文章

【翻译】在Ext JS 5种使用ViewControllers

原文&#xff1a;Using ViewControllers in Ext JS 5简介在Ext JS 5中&#xff0c;在应用程序架构方面提供了一些令人兴奋的改进&#xff0c;如添加了ViewModels、MVVM以及viewControllers来加强MVC应用程序。最重要的是&#xff0c;这些选择并不互斥&#xff0c;因此&#xff…

10.StringBuffer常用方法

2019独角兽企业重金招聘Python工程师标准>>> 1. 照片 1 stringBuffer /** * stringBuffer: * 1. 构造方法 * 无参构造 StringBuffer sb new StringBuffer(); * 2. 添加功能 publ…

一步一步学习Redis——使用config命令查看或设置配置项

1.Redis配置 Redis 的配置文件位于 Redis 安装目录下&#xff0c;文件名为 redis.conf(Windows 名为 redis.windows.conf)。 你可以通过 CONFIG 命令查看或设置配置项。 2.Redis config命令格式 2.1 查看配置项 redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME 使…

mysql_cluster集群_Mysql-cluster集群

Mysql-cluster集群Master&#xff1a;192.168.1.210Data1:192.168.1.209Data2:192.168.1.208Node1:192.168.1.207Node2:192.168.1.206conf文件参考&#xff1a;vim /var/lib/mysql-cluster/config.ini ---------------------管理节点配置文件cat >/var/lib/mysql-cluster/co…

git提交代码到码云

git是代码管理工具&#xff0c;配合工具可以提交本地代码到GitHub&#xff0c;如果是国内用户可以提交代码到码云。具体工具的安装和码云的注册省略&#xff0c;下面简单的记录下如何提交一个代码到码云。 码云中新建仓库 &#xff08;1&#xff09;点击""图标新建仓…

Android中canvas rotate中心点问题图解

为什么80%的码农都做不了架构师&#xff1f;>>> 第一步平移&#xff1a; canvas.translate(200, 100); 第二步旋转90度 (注意&#xff1a;x 和 y 坐标会一同旋转) canvas.rotate(90); 第三步向x轴移动-100 canvas.translate(-100, 0); 第四步向x轴移动-100&#xf…

替代left join方法_义县游学电子教您一招:改变系统的快捷键映射为一个按键的方法...

01文章主题大家好&#xff0c;我是您的朋友义县游学电子科技.今天跟您介绍的文章主题是&#xff1a;改变系统的快捷键映射为一个按键的方法&#xff0c;原本需要按下ctrlA才能完成的全选功能&#xff0c;现在你只需要按下A键就能搞定了.是不是非常的省力而不麻烦.有了这个功能您…

一步一步学习Redis——五大数据类型(String、Hash、List、Set、ZSet)简要介绍

1.开篇 Redis支持五种数据类型&#xff1a;String&#xff08;字符串&#xff09;&#xff0c;Hash&#xff08;哈希&#xff09;&#xff0c;List&#xff08;列表&#xff09;&#xff0c;Set&#xff08;集合&#xff09;及ZSet(sorted set&#xff1a;有序集合)。 2.Redis…