QT:moveToThread与信号与槽机制的第五个参数Qt::ConnectionType

原来对QThread的理解,就是重写run(),曾经还一度搞不明白,到底它的槽属于主线程还是子线程。
后来学了MFC,一度觉得MFC的机制比较人性化,起码有工作线程和界面线程的用法,而不像QThread只有run是真正活在子线程里面的。
而直到今天再次研究QThread,发现QThread有很好的功能void QObject::moveToThread(QThread*);
先上代码:

widget.h

[cpp]  view plain  copy
  1. #ifndef WIDGET_H  
  2. #define WIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QDebug>  
  6. #include <QThread>  
  7.   
  8. class QPushButton;  
  9.   
  10. namespace Ui {  
  11. class Widget;  
  12. }  
  13.   
  14. class myObject : public QObject  
  15. {  
  16.     Q_OBJECT  
  17. public:  
  18.     myObject() {}  
  19.     ~myObject() {}  
  20.   
  21. public slots:  
  22.     void first()   
  23.     {  
  24.         qDebug()<< QThread::currentThreadId();  
  25.     }  
  26.     void second()   
  27.     {  
  28.         qDebug()<< QThread::currentThreadId();  
  29.     }  
  30.     void third()   
  31.     {  
  32.         qDebug()<< QThread::currentThreadId();  
  33.     }  
  34. };  
  35.   
  36. class Widget : public QWidget  
  37. {  
  38.     Q_OBJECT  
  39.       
  40. public:  
  41.     explicit Widget(QWidget *parent = 0);  
  42.     ~Widget();  
  43.       
  44. private:  
  45.     Ui::Widget *ui;  
  46.     myObject *my;  
  47.     QPushButton *firstButton,*secondButton,*thirdButton,*selfButton;  
  48.   
  49. public slots:  
  50.     void onFirstPushed();  
  51.     void onSelfPushed();  
  52. };  
  53.   
  54. #endif // WIDGET_H  


widget.cpp
[cpp]  view plain  copy
  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3. #include <QVBoxLayout>  
  4. #include <QPushButton>  
  5.   
  6.   
  7. Widget::Widget(QWidget *parent) :  
  8.     QWidget(parent),  
  9.     ui(new Ui::Widget)  
  10. {  
  11.     ui->setupUi(this);  
  12.     my = new myObject;  
  13.     firstButton = new QPushButton(tr("firstBtn"), 0);  
  14.     connect(firstButton, SIGNAL(clicked()), this, SLOT(onFirstPushed()));  
  15.     secondButton = new QPushButton(tr("secondBtn"), 0);  
  16.     connect(secondButton, SIGNAL(clicked()), my, SLOT(second()), Qt::DirectConnection);  
  17.     thirdButton = new QPushButton(tr("thirdBtn"), 0);  
  18.     connect(thirdButton, SIGNAL(clicked()), my, SLOT(third()), Qt::QueuedConnection);  
  19.     selfButton = new QPushButton(tr("selfBtn"), 0);  
  20.     connect(selfButton, SIGNAL(clicked()), this, SLOT(onSelfPushed()));  
  21.   
  22.     QVBoxLayout *layout = new QVBoxLayout;  
  23.     layout->addWidget(firstButton);  
  24.     layout->addWidget(secondButton);  
  25.     layout->addWidget(thirdButton);  
  26.     layout->addWidget(selfButton);  
  27.   
  28.     this->setLayout(layout);  
  29.   
  30.     QThread *thread = new QThread;  
  31.     my->moveToThread(thread);  
  32.     connect(thread, SIGNAL(started()), my, SLOT(first()));  
  33.     thread->start();  
  34. }  
  35.   
  36. Widget::~Widget()  
  37. {  
  38.     delete ui;  
  39. }  
  40.   
  41. void Widget::onFirstPushed()   
  42. {  
  43.     my->first();  
  44. }  
  45.   
  46. void Widget::onSelfPushed()   
  47. {  
  48.     qDebug() << QThread::currentThreadId();  
  49. }  
此代码参考自http://blog.csdn.net/sydnash/article/details/7425947 ,在度娘找的各种文章中,这篇算是最简洁易懂的,看了这篇再看其它都不成问题了。

    先说毫无疑问的两个功能:一是程序开始,线程启动,而my在线程中,my在线程中打印线程号;二是selfBtn,connect的槽函数完完全全属于主线程的函数,打印主线程线程号。
    接下来是三个重点的按钮,三个按钮的连接方式是不一样的。
    firstBtn连接的是主线程的槽函数,再在槽函数中执行first(),这样first()是在主线程中调用的,打印出来的是主线程的ID;
    secondBtn直接连接myObject中的槽函数,使用的是Qt:DirectConnection直接连接模式,此模式下信号与槽是同步的,信号发出后,直接在信号发送者线程中调用槽函数,由于信号是主线程发出的,因此打印的也是主线程的ID;
    thirdBtn也是直接连接myObject中的槽函数,但使用的是QT::QueuedConnection队列连接模式,此模式下信号与槽是异步的,信号发出后,会进入队列中,直到控制权到了接收对象(my)属于的线程(thread)的事件循环时,槽函数才被调用,因此此时打印的是子线程thread的线程ID。

    这里重点介绍下connect的第五个参数Qt::ConnectionType。此参数可以有三种选择Qt::AutoConnection、Qt::DirectConnection、Qt::QueuedConnection,分别是自动连接,直接连接和队列连接。正如上面所说,直接连接是同步的,槽函数将和信号同一线程,而队列连接是异步的,槽函数会属于接收对象所属线程。而自动连接是缺省选项,将自动选择直接连接和队列连接之一。而什么时候选择什么连接呢,发送者和接收者处于相同线程,选择直接连接;发送者和接收者处于不同线程,使用队列连接。(看过一篇很有趣的验证文,有兴趣可以参考http://www.cppblog.com/andreitang/archive/2011/08/26/154392.html

    对于上面代码而言QPushButton的三个connect都属于发送者和接收者不在同一个线程,不使用第五个参数的情况下,是默认使用队列连接的,而thread的connect属于发送者和接收者在同一线程,都是thread的线程,默认使用直接连接。
ps:需要注意的是,虽然在connect时,my由主线程生成,还没moveToThread,还在主线程,但在运行时,信号发出的时候,my已经在子线程了,因此自动连接下还是会选择队列连接。自动连接的选择并不是在于对象生成的线程,而是在于对象所处的线程决定的。

    好了总结完毕,准备将线程加入到项目中。。。
End

猜你喜欢

转载自blog.csdn.net/kaida1234/article/details/79557348