The signal and slot mechanism of the qt4 of learning notes __qt4

Source: Internet
Author: User
Tags emit
A signal is a specific identifier; a slot is a function, unlike a normal function, a slot function can be either associated with a signal or called directly as a normal function.
When an event occurs, by sending a signal, you can activate the slot function associated with it, that is, executing the slot function code. In a program, a signal is associated with a slot using the Qobject::connect () function, and the true association between the signal and the slot is realized by the signal and slot mechanism of QT.


There can be 3 modes of correlation between signals and slots:
-〉 a signal and a slot;
-〉 a signal and a plurality of slots;
-〉 multiple signals and a slot;
When a signal is associated with multiple slots, when the signal is emitted, the slots associated with the signal are executed immediately in any order. The signal and slot mechanisms are completely independent of the GUI event loop.
In addition, the signal can also be associated with the signal. When two signals are associated, the first signal is emitted to give the QT a second signal.
Signal and slot mechanisms via Qobject::connect (const QObject *sender,const char *signal,const QObject *receiver,const:: ConnectionType type=qt::autocompatconnection) function, the parameter type defines how the signal is associated with the slot, and whether a signal is passed immediately to the slot or queued for later delivery.


QT uses enumerated types Qt::connectiontype defines the way in which signals are associated with slots, with 3 kinds:
-〉QT::D irectconnection, the signal is sent immediately after the associated slot, only after the completion of the slot function returned to send the signal "emit< signal >" After the code is executed.
-〉qt::queuedconnection, the signal is sent to the queue until the event loop is able to pass him to the slot. Regardless of whether the slot function is executed, the code that sends the signal "emit< signal >" will be executed immediately.
-〉qt::autoconnection, if the signal and slot are on the same thread, after the signal is emitted, the slot function will execute immediately, equivalent to QT::D irectconnection; If the signal and slot are not on the same thread, the signal will queue, waiting for the process of the event loop, the effect is equivalent to QT :: Queuedconnection.

For qt::queuedconnection mode, the QT Meta object system must know the signal/slot parameter type, otherwise the compiler will complain "qobject::connect:cannot queue arguments of type ' type '", The type type you want to use must first be registered with the Qregistermettype () function in the Meta object System (simple data types and QT-defined data types are not registered and can be used directly).


The macro signal () and slot () return the C-style string (const char *) of its arguments, so the following two statements of the associated signal and slot are equivalent:
Connect (pushbutton,signal (clicked ()), This,slot (Dopushbutton ()));
Connect (pushbutton, "clicked ()", This, "Dopushbutton ()");

The advantages of QT signal and slot mechanism are: type safety, loose coupling.

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.