Signal and slot mechanism in QT

Source: Internet
Author: User
Tags emit

I. INTRODUCTION

As far as I can understand, the signal slot mechanism is similar to the message mechanism under Windows, the message mechanism is based on the callback function, QT uses the signal and slot to replace the function pointer, makes the program more safe and concise.

The signal and slot mechanism is the core mechanism of Qt, allowing programmers to bind unrelated objects together to achieve communication between objects.

    • Signal

When an object changes its state, the signal is emitted by the object (emit), and the object is only responsible for sending the signal, and it does not know who is receiving the signal at the other end. This enables true information encapsulation, ensuring that objects are used as a real software component.

    • Slot

Used to receive signals, and slots are just normal object member functions. A slot does not know if there is any signal connected to itself. And the object is not aware of specific communication mechanisms.

    • Signal-to-slot connection

All classes derived from Qobject or its subclasses (for example, qwidget) can contain signals and slots. Because the signal-to-slot connection is achieved through the Qobject connect () member function.

connect(sender, SIGNAL(signal), receiver, SLOT(slot));

Where sender and receiver are pointers to objects, SIGNAL () and slot () are macros that convert signals to slots.

Two. Features
    • One signal can be connected to multiple slots

When the signal is emitted, each slot is called one after another in an indeterminate order .

    • Multiple signals can be connected to the same slot

That is, no matter which signal is fired, this slot will be called.

    • Signals can be connected directly to each other

When the first signal is launched, a second signal is also emitted.

    • Connection can be removed

This situation is used less because QT automatically removes all connections related to this object when the object is deleted. The syntax is as follows:

disconnect(sender, SIGNAL(signal), receiver, SLOT(slot));
Three. Issues to be aware of
    1. The signal and slot mechanism is the same as the call of the normal function, and if used improperly, it is possible to generate a dead loop when the program executes. Therefore, it is important to avoid indirectly forming an infinite loop when defining the slot function, that is, the same signal received again in the slot. For example, in the example given earlier, if you add a statement emit mysignal () in the Myslot () slot function, you can form a dead loop.
    2. If a signal is associated with multiple slots, then when the signal is emitted, the associated slots will be activated in the order in which they are connected.
    3. Macro definitions cannot be used in signal and slot parameters.
    4. The number of parameters of the signal and slot must be the same as the type.

Iv. Examples of source code

#include "gettime.h"

#include"ui_gettime.h"
#include<QTime>
GetTime::GetTime(qwidget*parent):       
    Qmainwindow(parent),  
    UI(newUI::GetTime)     
{
    UI-Setupui(this);    
  connect (ui->pbtngettimesignal ()), Span style= "color: #808000;" >this,slot ( Currenttime          
    Connect(this,SIGNAL(getData(QString)),UI-Lbldisplaytime ,SLOT(setText(QString)));           
}
GetTime:: ~GetTime()  
{
    DeleteUI; 
}
voidGetTime::currenttime()   
{
    EmitgetData(qtime::currenttime().  ToString("Hh:mm:ss"));         
}

Signal and slot mechanism in QT

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.