Chapter 7 exploring the core mechanism of Qt-signal and slot, exploring qt

Source: Internet
Author: User

Chapter 7 exploring the core mechanism of Qt-signal and slot, exploring qt

Chapter 7 exploring the core mechanism of Qt-signal and slot

Note: To use Qt's core mechanism signal and slot, you must declare the Q_OBJECT macro in the private data zone of the class, and then the moc compiler will be responsible for reading the macro for code conversion, so that the unique mechanism of Qt can be used.

The so-called signal slot, in simple words, is like a plug-in: A plug and a socket. When an event occurs, such as clicking the mouse or pressing a button, the component returns a signal. If a slot corresponds to the signal, the slot function is called again.

There is no big difference between Slot functions and common c ++ member functions. They can also be virtual; they can be overwritten; they can be public, protected, or private; can be called by other c ++ functions; parameters can be of any type.The slot function can be connected to a signal. When this signal occurs, it is automatically called..

The connection signal and slot are the connect () function. The prototype is as follows:

Bool QObject: connect (const QObject * sender, const QMetaMethod & signal, const QObject * receiver, const QMetaMethod & method, Qt: ConnectionType type = Qt: AutoConnection) [static]

It can associate the signal sent by an object (sender) with the method of the receiver, in this way, the slot function associated with the signal is executed when the signal is generated.In the connect function, we use the two macros SIGNAL () and SLOT () provided by Qt. This is required by Qt. To associate signals and slots, we must use these two macros, the two macros are defined as follows:

# Define SLOT (name) "1" # name

# Define SIGNAL (name) "2" # name

With these two macros, we can convert the slot and signal name we pass into strings and add additional characters before these two strings.

For example:

Connect (ui-> pushButton, SIGNAL (clicked (), this, SLOT (myslots ()));

After moc processing, it becomes:

Connect (ui-> pushButton, "2 clicked ()", this, "1 myslots ()");

The last parameter type of the connect function can specify the method for transmitting signals. It is a constant of the Qt: ConnectionType Enumeration type. The following table lists the common connection types.

Constant Value Description
Qt: AutoConnection 0 When the signal sender and receiver are in the same thread, this type is equivalent to DirectConnection, and vice versa is equivalent to QueuedConnection. This type is also the default connection type of the connect function.
Qt: DirectConnection 1 Once a signal is transmitted, the associated slot function is executed immediately.
Qt: QueuedConnection 2 When a signal is generated, the signal is temporarily buffered into a message queue. Wait for the receiver's event to be processed cyclically to obtain the message from the queue, and then execute the slot function associated with the signal, in this way, messages can be transmitted in the same thread or cross-thread operations can be performed.
Qt: BlockingQueuedConnection 4 This type is similar to QueuedConnection, but it can only be applied to cross-thread operations, that is, the sender and receiver are in different threads, and the signal sender thread will block the wait for the receiver's slot function to end.
Qt: AutoCompatConnection 3 When Qt3 program compatibility is the default connection type

One signal can be connected to multiple slots (the slots are called one by one, but the call sequence is uncertain );

Multiple signals can be connected to one slot (this slot will be called once any signal is generated );

One signal can be connected to another signal;

The slot can be canceled:

Bool QObject: disconnect (const QObject * sender, const QMetaMethod & signal, const QObject * receiver, const QMetaMethod & method) [static]

This is not often the case, because after an object is deleted, Qt automatically removes all the slots connected to the object.

Signals and slots to be connected together. If parameters are included, the order and type of these parameters must be consistent, and the number of parameters in the signal band can be greater than that in the slot, when a signal is transmitted, it can pass the parameters to the slot function for receiving. This is also a method for interaction data between different objects. A signal can send multiple types of data to the slot function, however, not all slot functions need to be processed. In this way, the number of slot function parameters can be less than or equal to the number of signal parameters, but not more than the number of connected signal parameters.In order to correctly connect the signal slot, the parameter type and sequence of the signal and slot must be the same.

The following are two examples: one is to use the connect function to connect to the signal slot, and the other is to use the Qt Creator designer to set the signal slot (essentially connect, just graphical ).

Example 1:

 1 #include <QAPPlication> 2 #include <QPushButton> 3  4 int main (int argc, char *argv[]) 5 { 6       QApplication app(argc, argv);   7       QPushButton *button = new QPushButton("Close"); 8       QObject::connect(button, SIGNAL(clicked()), button, SLOT(close())); 9       button->show();10 11      return app.exec();12 }    

Enter the following command to compile:

Qmake-project

Qmake

Make

Run the program:

Click "Close" to Close the window.

Example 2:

Use Qt Creator to create a project

How to make the interface disappear immediately after the user clicks the "Close" button, of course, it is done by connecting clicked and close. Next, let's take a look at how it works? Double-click signalslotdialog. ui to enter the designer

Drag and Drop a Push Button to the space placement area, and modify the text attribute to "Close"

Click the "Edit" menu of Qt Creator and click "Edit signals/slots" in the pop-up sub-menu, when you move the cursor over the "Close" button, the color of the button changes. Click the button and drag the mouse to the blank area of the dialog box. The page shown is displayed.

When the left mouse button is released, the "Configure Connection" dialog box is displayed.

Set as shown in, click "OK"

In this way, the signal clicked and the slot close are connected.

Click compile to run the following interface

Click the Close button to exit.

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.