QT Learning Pathway (4): A preliminary study of signal groove

Source: Internet
Author: User
Tags event listener

I've seen the simple Hello, world!. After that, let's look at QT's most proud signal slot mechanism!

The so-called signal groove, in simple terms, is like a bolt: a Plug and a socket. What do you say? When an event occurs, for example, by clicking the mouse, or by pressing a button, the component emits a signal. It's like a radio, and if there's an event, it's audible all over the sky. At this point, if there is a slot that corresponds to the signal, then the function of this slot will be executed, that is, the callback. It's like a broadcast, and if you're interested, you'll respond to the broadcast. The dry explanation is weak, or read the code:

#include <QtGui/QApplication>
#include <QtGui/QPushButton>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton *button = new QPushButton("Quit");
QObject::connect(button, SIGNAL(clicked()), &a, SLOT(quit()));
button->show();
return a.exec();
}

This is a new file on the Qt creator, as it is explained in detail as to how to build a new project, so there is no more to repeat. This program is very simple, only a button, click after the program exit. (By the way, the button in Qt is called Qpushbutton, I don't understand why a simple button has to add a push?) Oh

The main thing is to look at this sentence:

Qobject::connect (Button, SIGNAL (clicked ()), &a, SLOT (Quit ()));

Qobject is the root of all classes. QT uses this qobject to implement a single root inherited C + +. It has a connect static function, which is used to connect the signal slot.

When a button is clicked, it emits a clicked signal, meaning that the surrounding components are declared: I was clicked! Of course, many of the other components are too lazy to ignore him. If interested in it, tell Qobject said, you help me stare point, as long as the button issued clicked Signal, you tell me-think about it, say, forget, you don't tell me, directly to execute my certain function! In this way, a signal slot is formed. Specifically, this example is Qapplication's example a says that if the button emits a clicked signal, you go to execute my quit function. So, when we click on the button, the Quit function of a is called and the program exits. So here, clicked () is a signal, and quit () is a slot, which is, figuratively speaking, inserting this signal into the slot.

QT uses the signal slot mechanism to complete the event listener operation. This is similar to the listener mechanism in swing, just a lot simpler than this listener. Later we will see that the definition of this signal slot is also unusually simple. It is noteworthy that this signal slot mechanism is just the use of the Qobject connect function, the other does not have any coupling-that is, you can use this mechanism to achieve your own signal monitoring! However, this requires the use of Qmake preprocessing!

Careful you may find, in Qt creator inside, signal and slot unexpectedly change color! Yes, QT does take them as a keyword! In fact, QT is the use of them to extend the C + + language, so it is necessary to use qmake for preprocessing, compared to the normal C + + compiler can be successfully compiled. In addition, there is no relationship between the signal and the signal inside the UNIX system! Oh oh, there is a connection, that is the name is the same!

Signal slot mechanism is one of the key parts of QT, we will discuss this problem carefully.

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.