qt之线程
第一种创建:
mythread1.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include<QThread>
#include<QDebug> class mythread:public QThread
{
public:
mythread(const QString & s,QObject *parent=nullptr);
void run();
void working();
private:
const QString &str;
}; #endif // MYTHREAD_H
mythread1.cpp:
#include "mythread.h"
#include<QDebug>
mythread::mythread(const QString & s,QObject *parent)
:QThread (parent),str(s)
{ }
void mythread::run()
{
working();
//exec();
}
void mythread::working()
{
for(int i=0;i<10;i++)
{
qDebug()<<str<<i<<QThread::currentThreadId()<<endl;
}
}
main.cpp:
#include "widget.h"
#include <QApplication>
#include "mythread.h"
#include<QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mythread my("s1");
qDebug()<<"主线程在运行"<<endl;
my.start();
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<"主线程在运行"<<endl;
qDebug()<<my.wait()<<endl;
//Widget add;
//add.show();
// return a.exec();
}
效果:

第二种创建:
,mythread2.h:
#ifndef MYTHREAD2_H
#define MYTHREAD2_H #include <QObject> class MyThread2 : public QObject
{
Q_OBJECT
public:
explicit MyThread2(const QString& s,QObject *parent = nullptr); signals: public slots:
void working1();
void working2();
private:
QString str;
}; #endif // MYTHREAD2_H
mythread2.cpp:
#include "mythread2.h"
#include<QDebug>
#include<QThread>
MyThread2::MyThread2(const QString& s,QObject *parent)
: QObject(parent),str(s){}
void MyThread2::working1()
{
for(int i=0;i<10;i++)
{
qDebug()<<str<<i<<QThread::currentThreadId()<<"working1"<<endl;
}
}
void MyThread2::working2()
{
for(int i=0;i<10;i++)
{
qDebug()<<str<<i<<QThread::currentThreadId()<<"working2"<<endl;
}
}
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include<QThread>
#include <QWidget>
#include<QPushButton>
#include "mythread2.h"
class Widget : public QWidget
{
Q_OBJECT public:
Widget(QWidget *parent = 0);
~Widget();
private: MyThread2 * mythread;
QThread * ms;
}; #endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include<QHBoxLayout>
#include<QThread>
#include<QObject>
#include<QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *s=new QHBoxLayout(this);
QPushButton *s1=new QPushButton("确定");
QPushButton *s2=new QPushButton("取消");
s->addWidget(s1);
s->addWidget(s2);
mythread=new MyThread2("mythread is starting...");
ms=new QThread(this);
mythread->moveToThread(ms);
ms->start();
connect(s1,SIGNAL(clicked()),mythread,SLOT(working1()));
connect(s2,SIGNAL(clicked()),mythread,SLOT(working2()));
connect(ms,SIGNAL(finished()),mythread,SLOT(deleteLater()));
} Widget::~Widget()
{
qDebug()<<"~Widget()"<<endl;
ms->quit();
ms->wait();
}
main.cpp:
#include "widget.h"
#include <QApplication>
#include "mythread.h"
#include<QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//mythread my("s1");
//qDebug()<<"主线程在运行"<<endl;
//my.start();
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<"主线程在运行"<<endl;
//qDebug()<<my.wait()<<endl;
Widget add;
add.show();
return a.exec();
}
效果:


qt之线程的更多相关文章
- Qt经典—线程、事件与Qobject(耳目一新)
介绍 You’re doing it wrong. — Bradley T. Hughes 线程是qt channel里最流行的讨论话题之一.许多人加入了讨论并询问如何解决他们在运行跨线程编程时所遇到 ...
- Qt同步线程(比较清楚,而且QMutex QMutexLocker QReadWriteLock QSemaphore QWaitCondition 每个都有例子)
Qt同步线程 我们知道,多线程有的时候是很有用的,但是在访问一些公共的资源或者数据时,需要进行同步,否则会使数据遭到破坏或者获取的值不正确.Qt提供了一些类来实现线程的同步,如QMutex,QMute ...
- [转]QT子线程与主线程的信号槽通信-亲测可用!
近用QT做一个服务器,众所周知,QT的主线程必须保持畅通,才能刷新UI.所以,网络通信端采用新开线程的方式.在涉及到使用子线程更新Ui上的控件时遇到了点儿麻烦.网上提供了很多同一线程不同类间采用信号槽 ...
- Qt 的线程与事件循环
Qt 的线程与事件循环
- Qt经典—线程、事件与Qobject
介绍 You’re doing it wrong. — Bradley T. Hughes 线程是qt channel里最流行的讨论话题之一.许多人加入了讨论并询问如何解决他们在运行跨线程编程时所遇到 ...
- Qt 子线程更新Ui
最近做练习,写一个Qt版的飞机大战,需要用子线程更新UI,发现Qt子线程不能更新Ui,否则程序会崩溃.在网上百度了下,说是需要在子线程自定义信号,然后在线程回调的run()函数里发射信号,主线程连接信 ...
- Qt同步线程(QMutex QMutexLocker QReadWriteLock QSemaphore QWaitCondition )
Qt同步线程 我们知道,多线程有的时候是很有用的,但是在访问一些公共的资源或者数据时,需要进行同步,否则会使数据遭到破坏或者获取的值不正确.Qt提供了一些类来实现线程的同步,如QMutex,QMute ...
- Qt之线程基础
何为线程 线程与并行处理任务息息相关,就像进程一样.那么,线程与进程有什么区别呢?当你在电子表格上进行数据计算的时候,在相同的桌面上可能有一个播放器正在播放你最喜欢的歌曲.这是一个两个进程并行工作的例 ...
- Qt跨线程信号和槽的连接(默认方式是直连和队列的折中)
Qt支持三种类型的信号-槽连接:1,直接连接,当signal发射时,slot立即调用.此slot在发射signal的那个线程中被执行(不一定是接收对象生存的那个线程)2,队列连接,当控制权回到对象属于 ...
- Qt新建线程的方法(四种办法,很详细,有截图)
看了不少Qt线程的东西,下面总结一下Qt新建一个线程的方法. 一.继承QThread 继承QThread,这应该是最常用的方法了.我们可以通过重写虚函数void QThread::run ()实现我们 ...
随机推荐
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- Order Statistic
目录 The Order Statistic 引理1 的一些基本性质 顺序统计量的分布 顺序统计量的条件分布 特殊分布的特殊性质 Order Statistic The Order Statistic ...
- Adversarially Robust Generalization Requires More Data
目录 概 主要内容 高斯模型 upper bound lower bound 伯努利模型 upper bound lower bound Schmidt L, Santurkar S, Tsipras ...
- CS5213高性价比替代AG6200芯片|兼容台湾AG6200芯片|CS5213Capstone
CS5213是一款HDMI转VGA带音频信号转出的芯片方案,CS5213支持HDCP协议,且外围电路比台湾安格AG6200要少,且本身CS5213芯片成本比AG6200要低,整个方案设计简单性价比较高 ...
- 快看!❤️又一超实用浏览器插件!常用网站自动整合,JSON格式化,CSDN全站去广告!多种工具一键调用。开发者的福音!
其实这个插件才出来的时候博主也下载了使用过,并没有什么亮点,那时候甚至觉得有点多余,因为CSDN全站去广告啥的,早就安装了油猴脚本,广告?不存在的嘿嘿.. 就在前几天看见CSDN的活动在推荐这款插件, ...
- Java常用的开发库推荐
我是3y,一年CRUD经验用十年的markdown程序员 今天来讲讲来给大家聊聊开发提速的东西了:工具包.Lombok和常用库 01.什么是工具包 基本上,每个项目里都有一个包,叫做utils.这个 ...
- RSA非对称加密算法实现:Java
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- 大厂必问的JVM面试题
本文目录: 讲一下JVM内存结构? 程序计数器 虚拟机栈 本地方法栈 堆 方法区 运行时常量池 直接内存 Java对象的定位方式 说一下堆栈的区别? 什么情况下会发生栈溢出? 类文件结构 什么是类加载 ...
- 在安装pdfplumber时报错 Microsoft Visual C++ 14.0 is required.
在安装pdfplumber时报下列错误: 解决方法: 更新pip ,因为pip 版本太旧 来自为知笔记(Wiz)