"Windows Programming" micro-Technology Report II

Source: Internet
Author: User
Tags emit

Special studies on signals and slots

Introduction: Signals (signal) and slots (slots) are used for communication between objects. Signal / slot mechanism is an important feature of Qt. in graphical user interface programming, it is often necessary to notify the change of one widget to another, or to communicate between objects. In general graphical user interface programming, callback functions are used for inter-object communication, so that callbacks and processing functions are bundled together without the simplicity and flexibility of the signal and slot mechanisms .

Content Description: Qt widgets have many predefined signals, andslots are functions that can be called to handle a particular signal. Qt Widgets also have many predefined slots, and when a particular event occurs, a signal is emitted, and the slot that is interested in the signal calls the corresponding response function. The signal / slot mechanism is implemented in the Qobject class, and all classes inherited from the Qobject class or one of its subclasses (such as the Qwidget Class) can contain signals and slots. When the object changes their state, the signal is sent, and the object does not care if any other object receives the signal it emits. A slot is a normal member function of a class. Signals and slots can be connected arbitrarily through the Connect function. When a signal is emitted the slot it connects to is executed immediately, just like a normal function call.

with a signal and a slot. The QT class Foo declaration is as follows:

Class Foo:public Qobject

{

Q_object //classes containing signals and / or slots must be declared Q_object

Public

Foo ();

int value () const {return val;}

Public Slots:

void SetValue (int);   // slot declaration

Signals:

void valuechanged (int);   // Signal Declaration

Private

int Val;

};

void Foo::setvalue (int v)

{

if (v! = val) {

val = v;

Emit valuechanged (v); Send Signal

}

}

The following two objects are connected together:

Foo A, B;

Connect (&a, SIGNAL (valuechanged (int)), &b, slot (setValue (int)));// signal to slot connection

B.setvalue (11); A = = undefined b = = 11

A.setvalue (79); A = = b = = 79

B.value ();


calling A.setvalue (79) causes A to emit a valuechanged () signal, and B will receive the signal in its setValue () slot, which is called B.setvalue (79). The next B will emit the same valuechanged () signal, but since no slots are connected to the valuechanged () signal of B, nothing happens (the signal disappears).

signals, slots and emit is not C++ signals, slots emit These keywords, and then use the standard c++ compiler. Run the moc Meta Object Compiler) for class definitions that contain signals and slots ). Generates a c++ source file that can be compiled and connected to other object files and referenced programs.

a slot is a normal member function, which is divided into public,protected, and private classesas normal member functions, public slots A slot that declares that any signal can be connected. protected Slots indicates that the slots of this class and its subclasses can be connected. The private slots indicates that the signal of the class itself can be connected to the slot of this class.

"Windows Programming" micro-Technology Report II

Related Article

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.