1.3 preliminary understanding of signals and slots, 1.3 preliminary understanding of Signals

Source: Internet
Author: User

1.3 preliminary understanding of signals and slots, 1.3 preliminary understanding of Signals

In Qt, how to respond to actions. This will use the Qt signal and slot mechanism.

My understanding: It is similar to the Message response mechanism of Win32 programs.

Signal: When we click a button, the button itself generates a message called "click", which indicates that we just clicked this button. This information is equivalent to sending a signal, indicating that a user action has occurred or the status has changed. This is the signal ).

It can be understood that when this button emits this signal, if a "invigilator" discovers this signal, it will immediately respond and perform the corresponding action. This action generally refers to executing one or more functions. These are slots ).

To make the slot respond to a specified signal, connect the signal to the specified slot. After the connection, as long as this signal appears, the slot will be automatically executed.

 

Next, let's take a look at the following code:

Row 7th: Create a QPushButton object, header file <QPushButton>

Functions used:QPushButton ::QPushButton(ConstQString&Text,QWidget*Parent= 0)

Function Description: Construct a text button. parent is its parent window. The default value is 0.

Line 3: connect the signal (clicked () to the quit.

Function used: boolQObject ::Connect(ConstQObject*Sender, ConstChar*Signal, ConstQObject*Cycler, ConstChar*Method,Qt: ConnectionTypeType= Qt: AutoConnection)

Function Description: Creates a connection of the specified type, specifying the sender object and method, and the receiver object and method. If the connection is successful, true is returned. Otherwise, false is returned.

The type parameter already has a default value. You do not need to specify it yourself.

Sender is the object for transmitting signals. signal is the specific SIGNAL sent by this object and must use the signal () Macro.

The receiver is the object for receiving signals, and the method is the SLOT, that is, the response function. The SLOT () macro must be used.

 

SIGNAL () and SLOT () are macros defined in Qt. These two macros convert their parameters into corresponding strings.

Both macro parameters are function names without parameter names (parameter names refer to variable names in function parameters, not parameter types, in fact, it can be said that the two macro parameters are something similar to the function pointer type? But there is no (*) followed by the return value and the return value ).

The sender and receiver objects each have their own transmit signals and slots. For example, QPushButton has clicked () signals and QApplication has quit () slots.

 

The signal of QPushButton is inherited from the qmeanactbutton class, which has the following signal:

One signal is inherited from QWidget and QObject. The main difference is the above four signals.

Just look at it ~ Hey, you can try it yourself.

 

QApplication has the following slots:

In this example, a quit () is used, which is obtained by the QApplication inherited from the QCoreApplication class.

In QCoreApplication, The quit () slot is defined.

 

In this example, the clicked () signal indicates the signal that will be sent when the user clicks the button, and then runs the quit () in the app object to exit the program.

 

Now, we have a preliminary understanding of signal and slot. This is the basis of Qt programming.

In addition, the Qt help document is also a good thing.

If you want to know the details of a Qt class using Qt Creator, click the class name and Press F1 to go to the help document.

 

In my opinion, the signal and the slot are better understood. Simply think of it as a sender and receiver. When you click the button, the sender sends a clicked () after receiving the message, the receiver starts to do its own action.

 

Of course, after all, it's just my summary note. In some cases, I understand it. If it's wrong, please forgive me ~

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.