QT signal slot connection syntax is summarized

Qt signals and slots is one of the lead frame that mechanism ho.

The so-called channel signal, is the actual observer mode . When an event occurs, for example, a button is clicked detected himself a bit, it will send a signal (signal). This trigger is not the purpose of a similar broadcast. If you are interested in this subject there is a signal, it will use the connection (connect) function, which means that the signal is a function of its own and will want to process (called slot (slot)) to bind to deal with this signal. That is, when the signal is issued, the slot function is automatically connected to the callback. This is similar to the observer pattern: When an event of interest has occurred, one operation is automatically triggered. (Mention here a signal Qt groove using additional processing to achieve, not GoF classic implementation of the observer pattern.)

Qt signals and slots are unique to the information transmission mechanism is an important foundation for Qt Designer program, which allows objects interfere with each other to establish a link.

It is the essence of the groove member function, which may be any type of parameter. And ordinary C ++ member function is almost no difference, it can be a virtual function; also can be overloaded; can be public, protected, private, and can be called by other C ++ member functions. The only difference is that: the groove can be connected to the signal together, every time the signal is transmitted and a slot connection, it calls the groove .

Signal is connected to the groove:

 C++ Code 
1
2
3
4
5
6
7
8
 
static  QMetaObject :: Connection
Connect (
    
const  QObject * SENDER,                           // a parameter: Sender
const  QMetaMethod & Signal,                       // two parameters: signal const  QObject * Receiver,                         // three parameters: the receiver const  QMetaMethod & Method,                       // four parameters: groove     the Qt :: :: AutoConnection the Qt = the ConnectionType type);   // five parameters: connection type     
    
    

Note : request signal and groove signal parameters consistent groove sistency, it is the same parameter type. If not, the case is allowed, the parameter can be a function of the groove is less than the signal, even so, the order of those parameters must also be a function of the presence of groove signals and several front into line. This is because you can choose to ignore the signal transmitted data (that is, the parameters of the slot function is less than the signal) in the slot function, but can not say that the signal did not have the data, you have to use (that is, the groove in the slot function multi-function parameters than the signal, which is not allowed).

Writing mode signal slots:

  • Qt4 writing style

QPushButton* button = new QPushButton("Quit");

connect(button, SIGNAL(clicked()), &a, SLOT(quit()));

The wording does not compile error, but gives an error at run-time, will undoubtedly increase the instability of the program.

Will only prompt function does not exist groove running in Debug mode, it will not give any error messages when running under Release mode.

  • Qt5 writing style

QPushButton button("Quit");

QObject::connect(&button, &QPushButton::clicked, &app,&QApplication::quit);

  • C ++ 11 new way: Lambda expressions, Pro project files need to add CONFIG + = C ++ 11

QPushButton* button = new QPushButton("Quit");

connect(button, SIGNAL(clicked()),[=](QString str){

                 qDebug() << str;});

About Lambda expressions of knowledge :

Simply put, Lambda expression is an anonymous function.

In square brackets [] to start, referred to as the introducer Lambda expressions.

Introducer may be appended Lambda expressions return type.

Followed by a list of parameters, and finally the function body Lambda expressions.

Introducer body function describing how to "get" external variable!

The so-called "external variables" refers to variables other than the body of the function of these variables need to be defined in the scope of the introducer visible.

Lambda expressions configuration diagram:

  • Capture clause: clause capture
  • Parameter list: a list of optional parameters
  • Mutable specification Optional
  • Exception specification Optional
  • Return type: optional return type
  • Lambda Body

需要注意的一点:在进行信号槽绑定时,如果有重载,需要对成员函数进行类型转换,可以使用 C++ 的 static_cast 类型转换(编译时进行语法检查),也可以使用传统的 C 语言的强制类型转换(编译时不进行语法检查,运行时才检查),或者 C++11 的 QOverload::of,C++14 的 qOverload:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
QComboBox *comboBox =  new  QComboBox();
comboBox->addItem(
"Michael" );
comboBox->addItem(
"Kobe" );
comboBox->addItem(
"James" );
comboBox->show();

// [1]
QObject::connect(comboBox,
                 
static_cast < void  (QComboBox::*)( int )>(&QComboBox::activated),
                 [](
int  index)
{
    qDebug() << index;
});

// [2]
QObject::connect(comboBox,
                 
static_cast < void  (QComboBox::*)( const  QString &)>(&QComboBox::activated),
                 [](
const  QString &text)
{
    qDebug() << text;
});

// [3]: QOverload<> 里面是参数列表,of() 里面是成员函数地址
QObject::connect(comboBox,
                 QOverload<
const  QString &>::of(&QComboBox::activated),
                 [](
const  QString &text)
{
    qDebug() << text;
});

Guess you like

Origin www.cnblogs.com/MakeView660/p/11941653.html