Qt Quick Start: Signal and slot

Source: Internet
Author: User

Qt Quick Start: Signal and slot

Signals and slots are mainly used for communication between components, similar to delegation in. net and java.

Use the QObject: connect method to associate the signal with the slot. Then the signal initiator sends a signal, and the slot function in the receiver executes.

For example, connect (this, SIGNAL (start (), worker, SLOT (Start () associates the start SIGNAL in the current class with the Start function of the worker object, when we call emit this-> start (), the slot function will receive this signal.

The connect function has the fifth parameter, which determines when the signal will be sent to the slot. Here I will mention it, but I have always used the default value.

Qt: AutoConnection. The default value is automatic connection. When the signal initiator is in the same thread as the slot function executor, Qt: DirectConnection is used, and Qt: QueuedConnection is used.

Qt: DirectConnection, which is directly connected. The signal initiating slot function is executed immediately, and the signal initiator and performer are in the same thread.

Qt: QueuedConnection, queue type. The signal initiator and executor are not in the same thread. When the signal is initiated, the slot function will wait for scheduling.

Qt: BlockingQueuedConnection, which is the same as Qt: QueuedConnection. The difference is that there is a mutex lock when the signal is initiated. Wait for the return of the slot function, in this way, if the slot function and the signal function are in the same thread, a deadlock will occur (that is, they are stuck in the lock position and cannot enter the slot function ).

Qt: UniqueConnection, Which is used together with the above through "or", indicates that the same connection can only be connected once, and it seems useless, multiple connections with the same object, signal, and slot will fail.

To disconnect the signal from the slot, use the QObject: disconnect method.

There are so many signal and slot usage, but they are often used, especially in multithreading, when the slot function is not executed, it may be that the connection is wrong or the thread where the slot function is located is blocked (such as an endless loop ).

Related Article

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.