[Qt] 2.2 continue to understand the signal and slot, and qt2.2 understand the signal
Like common member functions, slots can be virtual functions, overloaded functions, public, private, and protected functions. It can be called by other C ++ member functions.
The slot connects to the signal. When this signal is sent, the slot is automatically called.
Connection function:
BoolQObject ::Connect(ConstQObject*Sender, ConstChar*Signal, ConstQObject*Cycler, ConstChar*Method,Qt: ConnectionTypeType= Qt: AutoConnection)
Sender and consumer er are pointers to QObject.
Signal and method are functions without parameter names and must use macro SIGNAL () and SLOT ().
One signal can be connected to multiple slotsFor example:
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox2, SLOT (click ()));
When you click okButton, both checkBox1 and checkBox2 execute their respective slot click ().
Multiple signals can be connected to the same slotFor example:
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
Connect (cancelButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
When you click okButton or cancelButton, checkBox1 will execute the slot click ().
One signal can be connected to another signalFor example:
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
Connect (cancelButton, SIGNAL (clicked (bool )),
OkButton, SIGNAL (clicked (bool )));
When you click cancelButton, The okButton signal clicked (bool) will respond, and okButton will emit the signal clicked (bool), so that the checkBox1 slot click () will be automatically executed.
In addition, the bool parameter in the clicked (bool) signal of the cancelButton is passed to the bool parameter in the clicked (bool) signal of the okButton.
Here, the okButton's clicked (bool) signal is used as a slot. The number of parameters in the slot requires at least how many parameters should be included in the function for transmitting signals, otherwise, the connection fails.
The connection can be removed.For example:
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
Disconnect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
When an object is deleted, Qt automatically deletes all connections related to the object.
The number of signal parameters must be at least the number of Slot parameters.
1. For example:
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (click ()));
When a signal is sent, the bool parameter of the signal is useless.
2. For example:
Connect (okButton, SIGNAL (clicked (bool )),
CheckBox1, SLOT (setHidden (bool ));
When a signal is sent, the bool parameter of the signal is passed to the bool parameter of the slot.
3. For example:
Connect (checkBox1, SIGNAL (released ()),
CancelButton, SLOT (setVisible (bool )));
This is incorrect. The slot requires a bool parameter, and the signal must also have this bool parameter.
4. For example:
Connect (this, SIGNAL (send (bool, int, double )),
This, SLOT (recive (bool, int )));
The number of signal parameters can be greater than or equal to the number of Slot parameters, and each parameter location of the signal and slot must correspond.
Here, the third double parameter of the signal is not transmitted to the slot.
Manually transmit signals in member functions and use emit, for example:
When an object is created, the constructor has connected the setVal signal to the recive slot.
When an object calls the SetValue () function, all setVal signals are sent and corresponding parameter values are input. After the signals are sent, the recive slot receives the signals and sets the values of the val variables.
Okay. Here is the summary of this section ~