Personal summary of QT signals and slots

Source: Internet
Author: User

1. Connect

[CPP]View Plaincopy
    1. Connect (sender,signal (SIGNAL)), Receiver,slot (SLOT ()));

Two macros are used here: SIGNAL () and slot (); The Connect declaration lets you know that the two macros end up with a const char* type.
You can see the macro definitions for signal () and slot () in Qobjectdefs.h:

[CPP]View Plaincopy
  1. #ifndef Qt_no_debug
  2. # define Qlocation "__file__": "Qtostring (__line__)
  3. # define METHOD (a) qflaglocation ("0" #a qlocation)
  4. # define SLOT (a) qflaglocation ("1" #a qlocation)
  5. # define SIGNAL (a) qflaglocation ("2" #a qlocation)
  6. #else
  7. # define METHOD (a) "0" #a
  8. # define SLOT (a) "1" #a
  9. # define SIGNAL (a) "2" #a
  10. #endif

So the role of these two macros is to convert the function name to a string and precede it with an identifier.

For example: SIGNAL (read ()) expands to "2read ()" and the same slot (read ()) expands to "1read ()".

[CPP]View Plaincopy
    1. Connect (sender,signal (SIGNAL)), Receiver,slot (SLOT ()));
    2. is actually connect (sender, "2signal ()", Receiver, "1slot ())";

2, Slots,signals

QT4 source code defines the following macros:

# define Slots

#define SIGNALS protected

# define Emit

Take another look at the example:

#ifndef Counter_h

#define Counter_h
#include <QObject>
Class Counter:public Qobject
{
    Q_object
Public
    Counter ();
Private
    int m_value1;
    int m_value2;
Public Slots:
    void setValue1 (int value);
    void setValue2 (int value);
Signals:
    void valueChanged1 (int newvalue);
    void ValueChanged2 (int newvalue);
};
#endif//Counter_h
Note that the public slots and signals defined by the header file are not meaningful to the C + + compiler and will be replaced with public and protected. Their true intentions are actually used by MOC tools, which are matched by the MOC based on these string keywords to generate file moc_counter.cpp. QT Source Construction process is the first MOC conversion and then execute the C + + compiler. The purpose of the MOC is to expand the signals and slots to generate a source file that can be read by the compiler.
The following is the generated MOC file:

Qt_begin_moc_namespace static const UINT Qt_meta_data_counter[] = {

 //content:        6,      //revision// Indicates the version number of the MOC generated code: QT4 's MOC generated code, which is 6, which is equivalent to MOC v6;qt5 7        0,       //ClassName index of the class name, the starting subscript for the array qt_meta_stringdata_counter is 0        0 ,    0,//ClassInfo        4,  ,//Methods//Has 4 functions (2 signal functions, 2 slot functions); The description of the function is labeled as the beginning of the array qt_meta_data_counter        0,    0,// Properties        0,    0,//enums/sets         0,    0,//constructors        0,       //Flags        2,      //Signalcount//number of signal functions

Signals:signature, parameters, type, tag, flags, 9, 8, 8, 0x05,//index of the signal, in the array qt_meta_stringdata_counte The starting subscript for R is 18,flags:0x05 signal, 9, 8, 8, 0x05,//Ibid.

Slots:signature, parameters, type, tag, flags, 8, 8, 0x0a,//slot index, from the array qt_meta_stringdata_counter The initial subscript is a 62,flags:0x0a that is the slot

8, 8, 0x0a,//Ibid.

0//EOD};

static const char qt_meta_stringdata_counter[] = {"counter\0\0newvalue\0valuechanged1 (int) \" "ValueChanged2 (int) \0value\0setvalue1 (int) "" "SetValue2 (int)"};

Article Source:

http://blog.csdn.net/oowgsoo/article/details/1529411 the signal and slot mechanism of QT source analysis

Realization of HTTP://WWW.DEVBEAN.NET/2012/12/HOW-QT-SIGNALS-AND-SLOTS-WORK/QT signal slot

http://woboq.com/blog/how-qt-signals-slots-work.html how QT signals and slots work

http://mobile.51cto.com/symbian-270982_all.htm qt Meta-object system and signal slot mechanism of QT source code

http://mobile.51cto.com/symbian-288016.htm analysis about QT Quick Macro Learning Tutorial
http://blog.csdn.net/superfpe/article/details/5782707 QT Signal and slot mechanism (a) ★

http://blog.csdn.net/superfpe/article/details/5788636 QT Signal and slot mechanism (II) ★

http://blog.csdn.net/ybjx111/article/details/8272405 QT qobject::connect Function Learning ★

http://blog.csdn.net/libaineu2004/article/details/19476039

Personal summary of QT signals and slots

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.