[QT Development] QT signal and slot example

Source: Internet
Author: User
Tags network function

Assume that the following scenario is based on:

In a program, there are two main logical functions: the UI interaction function block and the network function block used for network communication. When a new message arrives on the network, the network function block must notify the UI to display the refreshed information. When a user enters a new operation command or information, the UI function block transfers data by calling a network program.

In non-QT programs, we generally implement such programs through function callbacks or event listening mechanisms. For example, suppose there are Class A and Class B. Class A objects contain pointers to Class B objects and upload their own object pointers to Class B, in this way, the callback of the function can be implemented. Object A can call the public method of object B, and B can call the public method of object A according to the actual situation.

This method is similar to the observer mode in the general design mode, but the observer mode provides more abstract encapsulation. For example, the mutual passing of pointers between objects is based on dynamically bound object pointers.

Back to the question, we prefer to use the signal and slot mechanism in the QT program to solve this problem. Of course, the signal and slot mechanisms play far more roles than that.

Based on this mechanism, different objects can send or receive signals. The method of receiving signals is called a slot. In summary, it has several features: (1) Compared with the common callback mechanism, it does not have complex pointer objects, but only specifies the objects for sending and receiving signals. (2) one slot can receive signals from multiple objects, and one signal can also be connected to multiple slots. (3) In function calls across threads or processes, the signal and slot mechanism is more secure and flexible, while some unsafe explicit pointer calls are saved.

The following is a simple example to demonstrate how signals and slots work.

(1) define the signal

#include <QObject>class A : public QObject{    Q_OBJECTpublic:    A(QObject* parent = 0);    signals:    void message_arrive(QString msg);};

#include "a.h"A::A(QObject* parent) : QObject(parent){}

As a participant object in signal and slot, it must be a qobject object, that is, it must be derived from qobject. In addition, the macro q_object Declaration must be added to the class declaration. The macro q_object is used to implement several key functions required by the QT object model by default.

There are several features about the signal: (1) the signal definition keyword is signals and does not need to be modified by adding access-level keywords because the signal is always public by default, to facilitate communication between different classes. (2) signals are always declared without defining the body, just like pure virtual functions of C ++. (3) parameters transmitted between the signal and the slot must correspond. (4) The return type of the signal is always void, that is, no return value.

(2) Definition slot

#include <QObject>class B : public QObject{    Q_OBJECTpublic:    B(QObject* parent = 0);    public slots:    void reflash_text(QString msg);};
# Include "B. H "B: B (qobject * parent): qobject (parent) {} void B: reflash_text (qstring MSG) {// todo: receive signals and process parameters}

The access level of the slot can be set to public, proteced, or private, depending on the specific situation. At the same time, the corresponding function body needs to be implemented. Of course, the signal and

The slot can also be defined in the same object.

(3) connection signals and slots

Method call: qobject: connect (const qobject * asender, const char * asignal, const qobject * areceive, const char * asignal, QT: connectiontype Atype ). The last parameter is optional, indicating the connection type (the connection type is designed to be secure, for example, queuedconnection is generally used for signals and slot connection types between different threads ).

For the above example, you can connect as follows:

QObject::connect(a, SIGNAL(message_arrive(QString)), this, SLOT(reflash_text(QString)));

Where a is a pointer to an object of Class A; this points to an object of Class B, that is, this function connect () is called in B.

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.