今天给大家介绍三种QT里面使用多线程的方法

1、继承QThread并且重写run方法来实现多线程

 #ifndef MYQTHREAD_H
#define MYQTHREAD_H
#include <QMutex>
#include <QThread>
class myQThread : public QThread
{
public:
myQThread()
{
_isRunning = false;
}
void run()
{
while(true)
{
_mutex.lock();
if(!_isRunning)
{
qDebug() << "myqthread" << QThread::currentThreadId();
}else
{
_mutex.unlock();
break;
}
_mutex.unlock();
QThread::msleep();
}
_isRunning = false;
}
void stop()
{
_mutex.lock();
_isRunning = true;
_mutex.unlock();
this->quit();
this->wait();
} volatile bool _isRunning;
QMutex _mutex;
}; #endif // MYQTHREAD_H

2、使用movetothread方法实现多线程,该类一定要继承QObject

#ifndef MYOBJECTTHREAD_H
#define MYOBJECTTHREAD_H #include <QObject> class myObjectThread : public QObject
{
Q_OBJECT
public:
explicit myObjectThread(QObject *parent = nullptr); signals: public slots:
void showID();
}; #endif // MYOBJECTTHREAD_H

3、使用c++11的thread来实现多线程

#ifndef MYC11THREAD_H
#define MYC11THREAD_H class myc11Thread
{
public:
myc11Thread(); void showc11ID(int i);
}; #endif // MYC11THREAD_H

4、来看一看这三种多线程如何使用

void showID(int i)
{
qDebug() << "全局函数的现成ID" << QThread::currentThreadId() << "i:" << i;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QThread重写runfangfa
tmpmyqthread = new myQThread();
tmpmyqthread->start(); //用movetothread,但是这个类要继承object
QThread *th = new QThread;
tmpmyobject = new myObjectThread();
//把tmpmyobject这个类的对象放到线程中
tmpmyobject->moveToThread(th);
//通过信号槽的方法,把槽函数运行在线程中
connect(th,SIGNAL(started()),tmpmyobject,SLOT(showID()));
th->start(); //C11原始的thread把全局函数初始化到线程
std::thread th1(showID,2);
th1.detach(); //C11原始thread把类的函数初始化到线程运行
tmpmyc11 = new myc11Thread();
std::thread th2(&myc11Thread::showc11ID,tmpmyc11,2);
th2.detach();
qDebug() << "mainThread:" << QThread::currentThreadId();
}

QT里面三种多线程介绍到这里

QT多线程的使用的更多相关文章

  1. Qt 多线程和网络编程学习

    一,Qt多线程类学习 QThread类,开始一个新的线程就是开始执行重新实现QThread::run(),run()是默认现实调用exec(),QThread::start()开始线程的执行,run( ...

  2. [转] Qt 多线程学习

    Qt 多线程学习 转自:http://www.cnblogs.com/IT-BOY/p/3544220.html 最近的项目上用到了关于多线程的知识,自己也比较感兴趣,所以就拿了那本<C++ G ...

  3. QT多线程笔记

    1.QT多线程涉及到主线程和子线程之间交互大量数据的时候,使用QThread并不方便,因为run()函数本身不能接受任何参数,因此只能通过信号和槽的交互来获取数据,如果只是单方面简单交互数据还过得去, ...

  4. Qt多线程-总结QThread-QThreadPool-QtConcurrent

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-总结QThread-QThreadPool-QtConcurrent     本文 ...

  5. Qt多线程-QtConcurrent并行运算高级API

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QtConcurrent并行运算高级API     本文地址:http://tec ...

  6. Qt多线程-QThreadPool线程池与QRunnable

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QThreadPool线程池与QRunnable     本文地址:https:/ ...

  7. Qt多线程-QThread

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QThread     本文地址:http://techieliang.com/2 ...

  8. Qt 多线程同步与通信

    Qt 多线程同步与通信 1 多线程同步 Qt提供了以下几个类来完成这一点:QMutex.QMutexLocker.QSemphore.QWaitCondition. 当然可能还包含QReadWrite ...

  9. SeetaFaceQt:Qt多线程

    为什么要做多线程,说个最简单的道理就是我们不希望在软件处理数据的时候界面处于无法响应的假死状态.有些处理是灰常花时间的,如果把这样的处理放到主线程中执行,就会导致软件一条路走到底,要等到处理完才能接收 ...

  10. 【QT】 Qt多线程的“那些事”

    目录 一.前言 二.QThread源码浅析 2.1 QThread类的定义源码 2.2 QThread::start()源码 2.3 QThreadPrivate::start()源码 2.4 QTh ...

随机推荐

  1. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. [Swift]LeetCode547. 朋友圈 | Friend Circles

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  3. [Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  4. [Swift]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  5. python-os模块使用

    1.合并路径 os.path.join("c:\\music\\ap\\0","mav.mp3") 'c:\\music\\ap\\0\\mav.mp3' 2. ...

  6. 【Spark篇】---Spark中广播变量和累加器

    一.前述 Spark中因为算子中的真正逻辑是发送到Executor中去运行的,所以当Executor中需要引用外部变量时,需要使用广播变量. 累机器相当于统筹大变量,常用于计数,统计. 二.具体原理 ...

  7. Python内置函数(24)——frozenset

    英文文档: class frozenset([iterable]) Return a new frozenset object, optionally with elements taken from ...

  8. Docker中运行EOS FOR MAC

    基本要求以及依赖 安装 docker for mac ➡️ https://www.docker.com/products/docker-desktop docker需要7GB+内存.电脑右上角doc ...

  9. WebSocket(2)---实现游戏公告功能

    实现游戏公告功能 实现功能:游戏管理里发布游戏公告,其它游戏玩家页面能够马上接受到游戏公告信息. 下面直接上代码案例,这里主要展示关键代码,底部有源码. 一.案例 1.pom.xml文件 主要是添加s ...

  10. Netty 简介

    上文我们介绍了NIO和BIO的区别,NIO相对于BIO是一次很大的进步.但我们平时开发中并不会使用NIO.这是因为NIO在开发中存在以下问题 NIO的类库和API繁杂,使用麻烦,需要熟练掌握Selec ...