第一种创建:

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();
}

效果:

最新文章

  1. MySQL知识树-查询语句
  2. CentOS7 mono环境连接WCF
  3. android注意事项
  4. 取消vs2013在代码中的Reference数量功能
  5. TCP/IP——内外网IP+子网掩码作用+PING(网络总结)
  6. size()函数的使用
  7. Codevs 1172 Hankson 的趣味题 2009年NOIP全国联赛提高组
  8. microchip PIC芯片使用方法
  9. JDK安装目录下的src
  10. Bitcode问题
  11. 【CSS3】表格、列表
  12. ASP.NET Core轻松入门之Middleware管道模型
  13. (转载)SQL Server2008附加数据库之后显示为只读时解决方法
  14. 【转】Parcelable, Serializable,Cloneable,copyProperties
  15. iOS UI基础-8.0 UIAlertView使用
  16. Java_5.2 数组应用:*的打印
  17. arcsde10 postgresql8.3 服务停止问题
  18. Vue.js hello world
  19. JavaScript中map函数和filter的简单举例
  20. 【pyhon】黑客用字典暴力破解Zip文件密码原理性展示

热门文章

  1. echarts map 地图在react项目中的使用
  2. htmlunit设置只采集html,取消对css,javascript支持
  3. window串口之CreateFile打开串口号大于9返回错误ERROR_FILE_NOT_FOUND
  4. 【LeetCode】227. Basic Calculator II 解题报告(Python)
  5. JS常用的获取值和设值的方法
  6. java泛型中&lt;?&gt;和&lt;T&gt;
  7. Oracle 11g安装和PL/SQL连接完全解读(连接本地数据库)
  8. 计算机系统2-&gt;从芯片说起 | 芯片怎样诞生
  9. Tomcat 服务器的端口会与其他的服务器端口发生冲突,此时则需要修改 Tomcat 服务器的端口
  10. SpringBoot 原理分析、监控、项目部署