Characteristics and usage of QT signals and slots

1. Concept:

信号( Signal )就是在特定情况下被发射的事件 , 例如 PushButton 最常见的信号就是鼠标单
击时发射的 clicked() 信号。
槽( Slot )就是对信号响应的函数。槽就是一个函数,与一般的 C++函数是一样的,可以定义在类的任何部分( publicprivateprotected ),可以具有任何参数,也可以被直接调用。
槽函数与一般的函数不同的是 :槽函数可以与一个信号关联,当信号被发射时,关联的槽函数被自动执行 。

Qt's Meta-Object Compiler (MOC) is a preprocessor that converts these Qt-featured programs into a standard C++ compatible form before the source program is compiled, and then the standard C++ compiler to compile. This is why a Q_OBJECT macro must be added to a class that uses the signal and slot mechanism. Only by adding this macro can moc preprocess the signal and slot code in the class.

Regarding the use of signals and slots, there are some rules to pay attention to.
(1) A signal can be connected to multiple slots:

connect (spinNum, SIGNAL(valueChanged(int)), this , SLOT(addFun(int)) ;
connect (spinNum, SIGNAL(valueChanged (int)), this , SLOT(updateStatus (int));

When the signal and slot functions have parameters, the type of the parameter must be stated in the connect() function, but the parameter name does not need to be written.

(2) Multiple signals can be connected to the same slot:

connect (ui->rBtnBlue , SIGNAL (clicked()) , this, SLOT (setTextFontColor ()));
connect (ui->rBtnRed , SIGNAL (clicked()) , this , SLOT (setTextFontColor ())) ;
connect (ui->rBtnBlack , SIGNAL (clicked()), this , SLOT (setTextFontColor ()));

(3) A signal can be connected to another signal:

connect (spinNum, SIGNAL(valueChanged (int)), this , SIGNAL (refreshinfo(int)) ;

(4) Strictly speaking, the number and type of parameters of the signal and slot need to be consistent. At least the parameters of the signal cannot be less than the parameters of the slot
. If they do not match, a compilation error or a runtime error will occur.

(5) In classes that use signals and slots, the macro QOBJECT must be added to the class definition.

(6) When a signal is emitted, its associated slot function is usually executed immediately, just like a normal call to a function.
Only after all slot functions associated with the signal have been executed, the code following the signal is executed.

class QMyClass : public QObject
{
    
    
	QOBJECT//引用信号与槽时候,必须引用
public//构造函数
    Widget(QWidget *parent = nullptr);
    //析构函数
    ~Widget();
private//
protected:
	//
private slots:	
	//
}

2. Different parameter forms of connect() function:

(1)一种参数形式的函数原型是:
connect(sender , SIGNAL(signal()), receiver, SLOT (slot())) ;

Features: The macros SIGNAL() and SLOT() are used here to specify signal and slot functions, and if the signal and slot functions have parameters, the parameter types must also be specified.

connect (sender , SIGNAL(valueChanged (int)), this , SLOT(updateStatus (int) );
(2)另外一种参数形式的函数的原型是 :

Applicable objects: For signals and slots with default parameters (that is, the signal name is unique, there are no two signals with different parameters but the same name), you can use this function pointer form to associate, such as:

connect (lineEdit,&QlineEdit::textChanged ,this, &widget::on_textChanged);
(3)易错点:
而对于具有不同参数的同名信号就不能采用函数指针的方式进行信号与槽的关联,例如QSpinBox() 有两个 valueChanged()信号 :
在这里插入图片描述

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44236302/article/details/124310582