我是靠谱客的博主 大意百合,这篇文章主要介绍Qt201_QThread使用方法2:将任务类对象移动到子线程中步骤:Code:资源回收:,现在分享给大家,希望可以做个参考。

步骤:

  1. 创建一个新的类,从QObject派生
  2. 类中添加一个共有的成员函数,子线程要执行的逻辑业务
  3. 在主线程中创建一个QThread对象,这就是子线程的对象
  4. 在主线程中创建工作的类对象(不要指定父对象)
  5. 将mywork对象移动到创建的子线程中去(QObject::moveToThread()方法)
  6. 启动子线程( start() ),这时候线程启动了,但是移动到线程中的对象并没有工作
  7. 调用mywork类对象的工作函数,这时候,是在移动到的那个子线程中运行的

Code:

主线程:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "mythread.h" QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); QThread *thread1; QThread *thread2; QThread *thread3; MyThread *gen; BubbleSort *bubble; QuickSort *quick; ~Widget(); private slots: private: Ui::Widget *ui; signals: void starting(int num); }; #endif // WIDGET_H
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//widget.cpp #include "widget.h" #include "ui_widget.h" #include "mythread.h" #include <QString> #include <QDebug> #include <QThread> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //1,创建子线程对象 thread1 = new QThread; thread2 = new QThread; thread3 = new QThread; //2,创建任务类对象 gen = new MyThread; bubble = new BubbleSort; quick = new QuickSort; //3,将任务对象移动到子线程中 gen -> moveToThread(thread1); bubble -> moveToThread(thread2); quick -> moveToThread(thread3); //4,启动子线程 connect(this,&Widget::starting,gen,&MyThread::working); connect(ui->butStart,&QPushButton::clicked,[=] { emit starting(10000); thread1->start(); }); //5,将线程1返回的数据,交给线程2,3处理 connect(gen,&MyThread::sendArray,bubble,&BubbleSort::working); connect(gen,&MyThread::sendArray,quick,&QuickSort::working); connect(gen,&MyThread::sendArray,[=](QVector<int> list){ //启动其他两个子线程 thread2->start(); thread3->start(); for(int i=0;i<list.size();i++) { ui -> randList -> addItem( QString::number( list.at(i) )); } }); //将子线程2,3处理好返回的数据,在主线程显示 connect(bubble,&BubbleSort::sendArray,[=](QVector<int> list){ for(int i=0;i<list.size();i++) { ui ->buluList -> addItem( QString::number( list.at(i) )); } }); connect(quick,&QuickSort::sendArray,[=](QVector<int> list){ for(int i=0;i<list.size();i++) { ui -> quitList -> addItem( QString::number( list.at(i) )); } }); //释放资源 connect(this, &Widget::destroyed,this,[=]{ qDebug()<<QString::fromLocal8Bit("资源1释放中,,,,,,,,,,,,"); thread1 ->quit(); thread1 -> wait(); thread1 -> deleteLater(); if(thread1 == nullptr) qDebug()<<QString::fromLocal8Bit("资源1释放完毕"); qDebug()<<QString::fromLocal8Bit("资源2释放中,,,,,,,,,,,,"); thread2 ->quit(); thread2 -> wait(); thread2 -> deleteLater(); if(thread2 == nullptr) qDebug()<<QString::fromLocal8Bit("资源2释放完毕"); qDebug()<<QString::fromLocal8Bit("资源3释放中,,,,,,,,,,,,"); thread3 ->quit(); thread3 -> wait(); thread3 -> deleteLater(); if(thread3 == nullptr) qDebug()<<QString::fromLocal8Bit("资源3释放完毕"); }); } Widget::~Widget() { delete ui; }

子线程:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//mythread.h #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QVector> #include <QObject> class MyThread: public QObject { Q_OBJECT public: explicit MyThread(QObject *parent = nullptr); void working(int num); private: signals: void sendArray(QVector<int> array); }; class BubbleSort: public QObject { Q_OBJECT public: explicit BubbleSort(QObject *parent = nullptr); void working(QVector<int> array); private: void buluSort(QVector<int> &s); signals: void sendArray(QVector<int> array); }; class QuickSort: public QObject { Q_OBJECT public: explicit QuickSort(QObject *parent = nullptr); void Swap(int &a, int &b); public slots: void working(QVector<int> list); private: void quickSorts(QVector<int> &s, int l, int r); signals: void sendArray(QVector<int> array); }; #endif // MYTHREAD_H
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//mythread.cpp #include "mythread.h" #include <QElapsedTimer> #include <QDebug> #include <QString> #include <QThread> MyThread::MyThread(QObject *parent):QObject(parent) { } void MyThread::working(int num) { qDebug()<<QString::fromLocal8Bit("当前线程地址为:")<<QThread::currentThread(); QElapsedTimer timeLog; timeLog.start(); QVector<int > list; for(int i = 0; i < num; i++) { list.push_back(rand()%100000); } int time = timeLog.elapsed(); qDebug()<<QString::fromLocal8Bit("生成")<<num<<QString::fromLocal8Bit("个随机数用时 ")<<time<<QString::fromLocal8Bit(" 毫秒"); emit sendArray(list); } BubbleSort::BubbleSort(QObject *parent):QObject(parent) { } void BubbleSort::working(QVector<int> array) { //sleep(2); qDebug()<<QString::fromLocal8Bit("冒泡线程地址为:")<<QThread::currentThread(); QElapsedTimer timeLog; timeLog.start(); if(array.size() == 0) { qDebug()<<QString::fromLocal8Bit("冒泡 数据为零"); } buluSort(array); int time = timeLog.elapsed(); qDebug()<<QString::fromLocal8Bit("冒泡排序用时: ")<<time<<QString::fromLocal8Bit(" 毫秒"); emit sendArray(array); } void BubbleSort::buluSort(QVector<int> &s) { qDebug()<<QString::fromLocal8Bit("冒泡排序中"); int swp = 0; for(int i=0; i<s.size(); i++) { for(int j = 0; j < s.size() - i - 1; j++) { if(s[j]>s[j+1]) { swp = s[j]; s[j] = s[j+1]; s[j+1] = swp; } } } } QuickSort::QuickSort(QObject *parent):QObject(parent) { } void QuickSort::working(QVector<int> list) { //sleep(1); auto str = QString::fromLocal8Bit("选择排序线程地址为:"); qDebug()<< str << QThread::currentThread(); QElapsedTimer timeLog; timeLog.start(); if(list.size() == 0) { qDebug()<<QString::fromLocal8Bit("选择数据为零"); } quickSorts(list,0,list.size()); int time = timeLog.elapsed(); qDebug()<<QString::fromLocal8Bit("选择排序用时: ")<<time<<QString::fromLocal8Bit(" 毫秒"); emit sendArray(list); } void QuickSort::quickSorts(QVector<int> &data, int low, int high) { qDebug()<<QString::fromLocal8Bit("选择排序中"); int min = 0; for(int i=0;i<data.size();i++) { min = i; for(int j=i+1;j<data.size();j++) { if(data[min]>data[j]) { min = j; } } int temp = data[min]; data[min] = data[i]; data[i] = temp; } } void QuickSort::Swap(int &a, int &b) { int swp = a; a = b; b = swp; }

资源回收:

  1. 创建线程对象时,指定父对象。(QThread   t1 = new QThread (this) ;
  2. 在窗口类的析构函数中,释放资源。
  3. 通过Widget::destoryed信号来释放资源(见widget.cpp中 释放资源):

最后

以上就是大意百合最近收集整理的关于Qt201_QThread使用方法2:将任务类对象移动到子线程中步骤:Code:资源回收:的全部内容,更多相关Qt201_QThread使用方法2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部