Qt signal and slot sorting

1. The signal and slot mechanism is the core mechanism of QT. To be proficient in QT programming, you must have an understanding of signals and slots. Signals and slots are a high-level interface used for communication between objects. They are the core features of QT and an important difference between QT and other toolkits. Signals and slots are a communication mechanism defined by QT, which is independent of the standard C/C++ language. Therefore, to correctly process signals and slots, a QT tool called moc (Meta Object Compiler) must be used, which is a C++ preprocessor, which automatically generates additional code required for high-level event handling. When using signals and slots, declare Q_OBJECT first .

1. Public, private and protected cannot be added in front of signals for modification; slots can be added in front of them, because Qt says that slot functions can be used as ordinary functions.

2. The functions in the signals area must be of void type, and these signal functions do not have function bodies, that is to say, you cannot define these signal functions by yourself.

3. Macro definitions and function pointers cannot be used for parameters of signals and slots, and signals and slots cannot have default parameters.

2. Signals Signals are emitted by an object when a signal changes the internal state of its client or owner. Only the class that defines this signal and its derived classes can emit this signal. When a signal is emitted, its associated slot is executed immediately, just like a normal function call. The signal-slot mechanism is completely independent of all GUI event loops. The emit function (emit) returns only after all slots have returned. If there are multiple slots associated with a signal, then when the signal is emitted, these slots will be executed one after the other, but the order of their execution will be random and indeterminate, we cannot artificially Specifies which to execute first and which to execute later. The declaration of the signal is carried out in the header file. The signals keyword of QT indicates that it has entered the signal declaration area, and then you can declare your own signal. For example, the following three signals are defined:  signals:  void mySignal();  void mySignal(int x);  void mySignalParam(int x,int y);  In the above definition, signals is a keyword of QT, not C/ C++. The next line void mySignal() defines the signal mySignal, which carries no parameters; the next line void mySignal(int x) defines the signal mySignal with the same name, but it carries an integer parameter, which is somewhat similar to the one in C++ virtual function. Formally speaking, the declaration of a signal is the same as that of an ordinary C++ function, but the signal has no function body definition. In addition, the return type of the signal is void, so don't expect to return any useful information from the signal.  







Signals are automatically generated by moc, they should not be implemented in .cpp files. 

3. Slot 

Slots are ordinary C++ member functions that can be called normally. Their only speciality is that many signals can be associated with them. This slot is called when the signal associated with it is emitted. Slots can have parameters, but slot parameters cannot have default values
Like ordinary C++ member functions, slot functions are also divided into three types, namely public slots, private slots and protected slots. 
public slots: Slots declared in this area mean that all objects can connect signals to them. This is very useful for component programming, where you can create objects that don't know each other, and connect their signals and slots so that information can be passed correctly. 
protected slots: Slots declared in this area mean that the current class and its subclasses can connect signals to them.
private slots: Slots declared in this area mean that only the class itself can connect signals to it.
Slots can also be declared as virtual functions, which is also very useful. 
Slot declarations are also made in header files. For example, the following three slots are declared: 
public slots: 
void mySlot(); 
void mySlot(int x); 

void mySignalParam(int x,int y);

4. Summary of the situations encountered in use

(1) Two different classes in the same thread

 For example, if a panel has a button, it is hoped that when the btn is clicked, the slot function in another class (such as Controller) will respond.

Implementation process: panel.h

signals:
    void SignalClickCancelBtn();

panel.cpp

bool bConnect = false;
 bConnect=connect(m_btnCancel, &Dx3DPushButton::clicked,this, &DxRestoreSessionPanel::SignalClickCancelBtn);
 Q_ASSERT(bConnect);

controller.cpp

bool bIsConnect = false;
bIsConnect = connect(m_Panel,&DxRestoreSessionPanel::SignalClickCancelBtn,
			this,&DxBaseMainController::SlotClickRestoreSessionPanelCancelButton);
Q_ASSERT(bIsConnect); 

并实现

SlotClickRestoreSessionPanelCancelButton();

(2) 不同线程间用信号槽通信

和上面两个类通信没有什么区别,只要保证信号发出是在connect之后就可以了,还有在线程间使用信号槽进行通信时,需要注意必须使用元数据类型,Qt内生的元数据类型,如int double QString 等,如果要用自己定义的数据类型,需要在connect前将其注册为元数据类型。如:

qRegisterMetaType<Msg>("Msg");  



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772389&siteId=291194637