第一种创建:

mythread1.h:

  1. #ifndef MYTHREAD_H
  2. #define MYTHREAD_H
  3. #include<QThread>
  4. #include<QDebug>
  5.  
  6. class mythread:public QThread
  7. {
  8. public:
  9. mythread(const QString & s,QObject *parent=nullptr);
  10. void run();
  11. void working();
  12. private:
  13. const QString &str;
  14. };
  15.  
  16. #endif // MYTHREAD_H

mythread1.cpp:

  1. #include "mythread.h"
  2. #include<QDebug>
  3. mythread::mythread(const QString & s,QObject *parent)
  4. :QThread (parent),str(s)
  5. {
  6.  
  7. }
  8. void mythread::run()
  9. {
  10. working();
  11. //exec();
  12. }
  13. void mythread::working()
  14. {
  15. for(int i=0;i<10;i++)
  16. {
  17. qDebug()<<str<<i<<QThread::currentThreadId()<<endl;
  18. }
  19. }

main.cpp:

  1. #include "widget.h"
  2. #include <QApplication>
  3. #include "mythread.h"
  4. #include<QDebug>
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. mythread my("s1");
  9. qDebug()<<"主线程在运行"<<endl;
  10. my.start();
  11. qDebug()<<"主线程在运行"<<endl;
  12. qDebug()<<"主线程在运行"<<endl;
  13. qDebug()<<"主线程在运行"<<endl;
  14. qDebug()<<"主线程在运行"<<endl;
  15. qDebug()<<"主线程在运行"<<endl;
  16. qDebug()<<"主线程在运行"<<endl;
  17. qDebug()<<"主线程在运行"<<endl;
  18. qDebug()<<"主线程在运行"<<endl;
  19. qDebug()<<my.wait()<<endl;
  20. //Widget add;
  21. //add.show();
  22. // return a.exec();
  23. }

效果:

第二种创建:

,mythread2.h:

  1. #ifndef MYTHREAD2_H
  2. #define MYTHREAD2_H
  3.  
  4. #include <QObject>
  5.  
  6. class MyThread2 : public QObject
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit MyThread2(const QString& s,QObject *parent = nullptr);
  11.  
  12. signals:
  13.  
  14. public slots:
  15. void working1();
  16. void working2();
  17. private:
  18. QString str;
  19. };
  20.  
  21. #endif // MYTHREAD2_H

mythread2.cpp:

  1. #include "mythread2.h"
  2. #include<QDebug>
  3. #include<QThread>
  4. MyThread2::MyThread2(const QString& s,QObject *parent)
  5. : QObject(parent),str(s){}
  6. void MyThread2::working1()
  7. {
  8. for(int i=0;i<10;i++)
  9. {
  10. qDebug()<<str<<i<<QThread::currentThreadId()<<"working1"<<endl;
  11. }
  12. }
  13. void MyThread2::working2()
  14. {
  15. for(int i=0;i<10;i++)
  16. {
  17. qDebug()<<str<<i<<QThread::currentThreadId()<<"working2"<<endl;
  18. }
  19. }

widget.h:

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include<QThread>
  4. #include <QWidget>
  5. #include<QPushButton>
  6. #include "mythread2.h"
  7. class Widget : public QWidget
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. Widget(QWidget *parent = 0);
  13. ~Widget();
  14. private:
  15.  
  16. MyThread2 * mythread;
  17. QThread * ms;
  18. };
  19.  
  20. #endif // WIDGET_H

widget.cpp:

  1. #include "widget.h"
  2. #include<QHBoxLayout>
  3. #include<QThread>
  4. #include<QObject>
  5. #include<QDebug>
  6. Widget::Widget(QWidget *parent)
  7. : QWidget(parent)
  8. {
  9. QHBoxLayout *s=new QHBoxLayout(this);
  10. QPushButton *s1=new QPushButton("确定");
  11. QPushButton *s2=new QPushButton("取消");
  12. s->addWidget(s1);
  13. s->addWidget(s2);
  14. mythread=new MyThread2("mythread is starting...");
  15. ms=new QThread(this);
  16. mythread->moveToThread(ms);
  17. ms->start();
  18. connect(s1,SIGNAL(clicked()),mythread,SLOT(working1()));
  19. connect(s2,SIGNAL(clicked()),mythread,SLOT(working2()));
  20. connect(ms,SIGNAL(finished()),mythread,SLOT(deleteLater()));
  21. }
  22.  
  23. Widget::~Widget()
  24. {
  25. qDebug()<<"~Widget()"<<endl;
  26. ms->quit();
  27. ms->wait();
  28. }

main.cpp:

  1. #include "widget.h"
  2. #include <QApplication>
  3. #include "mythread.h"
  4. #include<QDebug>
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. //mythread my("s1");
  9. //qDebug()<<"主线程在运行"<<endl;
  10. //my.start();
  11. //qDebug()<<"主线程在运行"<<endl;
  12. //qDebug()<<"主线程在运行"<<endl;
  13. //qDebug()<<"主线程在运行"<<endl;
  14. //qDebug()<<"主线程在运行"<<endl;
  15. //qDebug()<<"主线程在运行"<<endl;
  16. //qDebug()<<"主线程在运行"<<endl;
  17. //qDebug()<<"主线程在运行"<<endl;
  18. //qDebug()<<"主线程在运行"<<endl;
  19. //qDebug()<<my.wait()<<endl;
  20. Widget add;
  21. add.show();
  22. return a.exec();
  23. }

效果:

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. vue-组件化编程

    1.传统编写方式和组件编写方式的区别 组件方式编写可以很方便的复用和封装某些功能模块/组件的命名最好语义化,方便维护和阅读 编写时,我们可以将某些共用的功能或者样式部分抽象,得到对应的组件,按需要引入 ...

  2. UDP&串口调试助手用法(5)

    note 提供安装包 基于win10开发 已通过win10测试,windows其他平台,没有测试 日志 2021-09-18 1.修复计算校验和错误的现象 2.屏蔽不计算校验和位置的REG验证(后期更 ...

  3. 一个自定义的c++错误类 和 同步异步、阻塞非阻塞(区别简述)

    一个例子,自定义exception 继承std::exception 1 class _oct_udp_api_export_ udp_err : public std::exception 2 { ...

  4. 【LeetCode】396. Rotate Function 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...

  5. 【LeetCode】908. Smallest Range I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  6. 第十个知识点:RSA和强RSA问题有什么区别?

    第十个知识点:RSA和强RSA问题有什么区别 这个密码学52件事数学知识的第一篇,也是整个系列的第10篇.这篇介绍了RSA问题和Strong-RSA问题,指出了这两种问题的不同之处. 密码学严重依赖于 ...

  7. $\infty$-former: Infinite Memory Transformer

    目录 概 主要内容 如何扩展? 实验细节 Martins P., Marinho Z. and Martins A. \(\infty\)-former: Infinite Memory Transf ...

  8. Java初学者作业——定义英雄类(Hero),英雄类中的属性包括:姓名、攻击力、防御力、生命值和魔法值;方法包括:攻击、介绍。

    返回本章节 返回作业目录 需求说明: 定义英雄类(Hero),英雄类中的属性包括:姓名.攻击力.防御力.生命值和魔法值:方法包括:攻击.介绍. 实现思路: 分析类的属性及其变量类型. 分析类的方法及其 ...

  9. docker学习:docker命令

    帮助命令 自验证 docker version 详情信息 docker info 获取帮助 docker --help 镜像命令 列出本例主机上的镜像 docker images [OPTIONS] ...

  10. [Azure DevOps] 管理测试计划、测试套件和测试用例

    我喜欢测试计划,它能让团队清楚测试进度,还能妥善分配测试人员,更重要的是它能保证测试质量和效率.Azure DevOps 里提供了 Test Plans 这个模块用于管理测试计划. 1. Azure ...