Qt::connectiontype (signal and Groove transfer mode)

Source: Internet
Author: User

Qt::autoconnection

Auto Connect: (default) If the signal is emitted within the thread that the receiver is attached to, it is equivalent to a direct connection. If the thread that emits the signal is different from the thread that the recipient is attached to, it is equivalent to a queue connection.

Qt::D irectconnection

Direct connection: When the signal is emitted, the slot function is called directly. The slot function executes within the thread that emits the signal, regardless of which thread the slot function belongs to.

Qt::queuedconnection

Queue connection: The slot function is called when control returns to the event loop of the thread to which the recipient is attached. The slot function executes on the thread attached to the receiver. In other words: this way, you can either pass messages within the thread or pass messages across threads

Qt::blockingqueuedconnection

Similar to qt::queuedconnection, but blocks until the associated slot is executed. Here is the word blocking, which means it is designed to deliver messages between multiple threads.

MyObject.h
#ifndef myobject_h#define myobject_h#include class myobject:public qobject{    q_objectpublic:    Explicit MyObject (Qobject *parent = 0);p ublic slots:    void Start ();}; #endif//Myobject_h
MyObject.cpp
#include "MyObject.h" #include #include myobject::myobject (qobject *parent)    : Qobject (parent) {}void MyObject:: Start () {    qdebug () << QString ("My Object thread ID:") << Qthread::currentthreadid ();}
Main.cpp
#include "MyObject.h" #include #include #include int main (int argc, char *argv[]) {    qapplication A (argc, argv);    Qdebug () << QString ("Main thread ID:") << Qthread::currentthreadid ();    MyObject object;    Qthread thread;    Object.movetothread (&thread);    Qobject::connect (&thread, SIGNAL (Started ()), &object, SLOT (Start ()));    Thread.Start ();    return a.exec ();}
To view the results of a run:

"Main thread ID:" 0xf08

"My Object Thread ID:" 0x216c

Obviously the thread of the main thread is different from the slot function (you can try it many times ... ), because the thread myobject is qthread after Movetothread, the started () signal is first emitted after the Thread.Start () described above, i.e. the started () signal is emitted in the secondary thread, So whether to take Qt::autoconnection, Qt::D irectconnection, qt::queuedconnection which way of connection, the main thread and the slot function of the threads are different.

1. Modify the code as follows:

    MyObject object;    Qthread thread;    Object.movetothread (&thread);    Qobject::connect (&thread, SIGNAL (Started ()), &object, SLOT (Start ()), Qt::D irectconnection);    Thread.Start ();
To view the results of a run:

"Main thread ID:" 0x2688

"My Object Thread ID:" 0x2110

Obviously the thread of the main thread and the slot function is different, the thread that MyObject is attached to is the main course (because commented out Movetothread), following the QT described above::D irectconnection (regardless of which thread the slot function belongs to, Slot functions are executed within the thread that emits the signal). That is, the started () signal is emitted in the secondary thread, the slot function is also in the secondary thread, so the main thread is different from the slot function.

2. Modify the code as follows:

    MyObject object;    Qthread thread;    Object.movetothread (&thread);    Qobject::connect (&thread, SIGNAL (Started ()), &object, SLOT (Start ()), qt::queuedconnection);    Thread.Start ();

To view the results of a run:

"Main thread ID:" 0x24ec

"My Object Thread ID:" 0x24ec

Obviously the thread of the main thread is the same as that of the slot function, following the qt::queuedconnection described above (the slot function executes on the thread attached to the receiver). That is, the started () signal is emitted in the secondary thread, but the thread that the MyObject is attached to is the main path (because the comment dropped movetothread), so the thread of the main thread and the slot function must be the same.

3. Modify the code as follows:

    MyObject object;    Qthread thread;    Object.movetothread (&thread);    Qobject::connect (&thread, SIGNAL (Started ()), &object, SLOT (Start ()), qt::autoconnection);    Thread.Start ();
To view the results of a run:

"Main thread ID:" 0x2700

"My Object Thread ID:" 0x2700

It is obvious that the thread of the main thread is the same as that of the slot function, and that the thread attached to the MyObject is the main course (because the comment dropped Movetothread), following the qt::autoconnection described above (if the signal is emitted within the thread attached to the receiver, it is equivalent to a direct connection. If the thread that emits the signal is different from the thread that the recipient is attached to, it is equivalent to a queue connection. )。 Because the started () signal is different from the thread that the MyObject is attached to, the result is the same as the qt::queuedconnection, so the thread of the main thread is the same as that of the slot function.

Qt::connectiontype (signal and Groove transfer mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.