第一种创建:

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之线程的更多相关文章

  1. Qt经典—线程、事件与Qobject(耳目一新)

    介绍 You’re doing it wrong. — Bradley T. Hughes 线程是qt channel里最流行的讨论话题之一.许多人加入了讨论并询问如何解决他们在运行跨线程编程时所遇到 ...

  2. Qt同步线程(比较清楚,而且QMutex QMutexLocker QReadWriteLock QSemaphore QWaitCondition 每个都有例子)

    Qt同步线程 我们知道,多线程有的时候是很有用的,但是在访问一些公共的资源或者数据时,需要进行同步,否则会使数据遭到破坏或者获取的值不正确.Qt提供了一些类来实现线程的同步,如QMutex,QMute ...

  3. [转]QT子线程与主线程的信号槽通信-亲测可用!

    近用QT做一个服务器,众所周知,QT的主线程必须保持畅通,才能刷新UI.所以,网络通信端采用新开线程的方式.在涉及到使用子线程更新Ui上的控件时遇到了点儿麻烦.网上提供了很多同一线程不同类间采用信号槽 ...

  4. Qt 的线程与事件循环

    Qt 的线程与事件循环

  5. Qt经典—线程、事件与Qobject

    介绍 You’re doing it wrong. — Bradley T. Hughes 线程是qt channel里最流行的讨论话题之一.许多人加入了讨论并询问如何解决他们在运行跨线程编程时所遇到 ...

  6. Qt 子线程更新Ui

    最近做练习,写一个Qt版的飞机大战,需要用子线程更新UI,发现Qt子线程不能更新Ui,否则程序会崩溃.在网上百度了下,说是需要在子线程自定义信号,然后在线程回调的run()函数里发射信号,主线程连接信 ...

  7. Qt同步线程(QMutex QMutexLocker QReadWriteLock QSemaphore QWaitCondition )

    Qt同步线程 我们知道,多线程有的时候是很有用的,但是在访问一些公共的资源或者数据时,需要进行同步,否则会使数据遭到破坏或者获取的值不正确.Qt提供了一些类来实现线程的同步,如QMutex,QMute ...

  8. Qt之线程基础

    何为线程 线程与并行处理任务息息相关,就像进程一样.那么,线程与进程有什么区别呢?当你在电子表格上进行数据计算的时候,在相同的桌面上可能有一个播放器正在播放你最喜欢的歌曲.这是一个两个进程并行工作的例 ...

  9. Qt跨线程信号和槽的连接(默认方式是直连和队列的折中)

    Qt支持三种类型的信号-槽连接:1,直接连接,当signal发射时,slot立即调用.此slot在发射signal的那个线程中被执行(不一定是接收对象生存的那个线程)2,队列连接,当控制权回到对象属于 ...

  10. Qt新建线程的方法(四种办法,很详细,有截图)

    看了不少Qt线程的东西,下面总结一下Qt新建一个线程的方法. 一.继承QThread 继承QThread,这应该是最常用的方法了.我们可以通过重写虚函数void QThread::run ()实现我们 ...

随机推荐

  1. AcWing 1113. 红与黑

    1.题目描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. 你站在其中一块黑色的瓷砖上,只能向相邻(上下左右四个方向)的黑色瓷砖移动. 请写一个程序,计算你总共能够到达多少块黑色的瓷砖 ...

  2. AcWing429. 奖学金

    题目: 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金. 期末,每个学生都有3门课的成绩:语文.数学.英语. 先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从 ...

  3. JAVA获取当前日期时间所在周的周一和周日日期

    /** * 获取当前时间所在周的周一和周日的日期时间 * @return */ public static Map<String,String> getWeekDate() { Map&l ...

  4. 【LeetCode】1579. 保证图可完全遍历 Remove Max Number of Edges to Keep Graph Fully Traversable

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 并查集 代码 欢迎加入组织 日期 题目地址:https ...

  5. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  6. 【LeetCode】506. Relative Ranks 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...

  7. Fence(poj1821)

    Fence Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4705   Accepted: 1489 Description ...

  8. 1119 机器人走方格 V2

    1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:131072 KB M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很大,只需要输出Mo ...

  9. C++单元测试框架gtest使用

    作用 作为代码编码人员,写完代码,不仅要保证编译通过和运行,还要保证逻辑尽量正确.单元测试是对软件可测试最小单元的检查和校验.单元测试与其他测试不同,单元测试可看作是编码工作的一部分,应该由程序员完成 ...

  10. 元宇宙(metaverse)中文社区-工程实践

    欢迎访问元宇宙中文社区,在这里大家可以提问,回答,分享,诉说,一起构建一个元宇宙社区. 2021年"元宇宙"的这个词的火热程度在业内绝对不亚于疫情,趁着这个热度,本文记录了如何搭建 ...