第一种创建:
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(); }
效果: