第一种创建:
mythread1.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
#include
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
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
#include "mythread.h"
#include
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
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
#include
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
#include
#include
#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
#include
#include
#include
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()"<
ms->wait();
}
main.cpp:
#include "widget.h"
#include
#include "mythread.h"
#include
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();
}
效果:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章