Qt Signals and Slots: A Comprehensive Guide
In Qt, signals and slots are a powerful mechanism for enabling loose coupling between objects. Unlike C++ member functions, which can only be virtual, reused, and public or private, signals and slots can be linked together, allowing for more flexibility and scalability.
Key Characteristics of Signals and Slots
- Optional Parameters: Signals and slots can have optional parameters, but the only difference is that signals and slots can be linked together, similar to events in C#.
- Connection: When connected, the signal will automatically call the slot, and the sender will send a pointer to the signal, while the receiver will receive a pointer to the slot.
- Sender and Receiver: The sender is a pointer to a QObject, and the receiver is a pointer to a QObject.
- Signal and Slot: The signal and slot are only parameters of the function name, and the SIGNAL and SLOT macros will be converted to the corresponding string.
Connecting Multiple Slots to a Signal
- Multiple Slots Connected to a Signal: A plurality of slots may be connected to a signal, and when the transmitted signal is uncertain, it will call the corresponding slot.
- Connecting Multiple Signals to a Slot: A plurality of signals can be connected to a slot, and when any of the connected signals are transmitted, it will call the slot.
- Connecting a Signal to Another Signal: A signal may be further connected to another signal, and when the connected signal is transmitted, it will call the slot of the connected signal.
Disconnecting Signals and Slots
- Disconnecting All Connections: All connections can be deleted using the QObject::disconnect() function.
- Disconnecting a Specific Connection: A specific connection can be deleted using the QObject::disconnect() function, specifying the sender, signal, receiver, and slot.
- Disconnecting Connecting Objects: The connection between two associated objects can be deleted using the QObject::disconnect() function, specifying the sender and receiver.
Best Practices for Using Signals and Slots
- Signal and Slot Matching: For a signal to make a successful connection to a slot (or a connection to another signal), they must have the same parameters and the same type of sequence.
- Ignoring Excess Parameters: If the parameter of the signal has more arguments than the slot, the remaining arguments are ignored.
Example Code
class myQt : public QMainWindow
{
Q_OBJECT
public:
myQt(QWidget *parent): QMainWindow(parent)
{
ui.setupUi(this);
QObject::connect(ui.btn_Open, SIGNAL(clicked()), this, SLOT(Open1()));
QObject::connect(ui.btn_Open, SIGNAL(clicked()), this, SLOT(Open2()));
}
private slots:
void Open1();
void Open2();
};
void myQt::Open1()
{
QMessageBox msg;
msg.setText("Open1!");
msg.exec();
}
void myQt::Open2()
{
QMessageBox msg;
msg.setText("Open2!");
msg.exec();
}
// Connecting multiple slots to a signal
QObject::connect(ui.btn_Open, SIGNAL(clicked()), this, SLOT(Open1()));
QObject::connect(ui.btn_ClickBool, SIGNAL(clicked()), this, SLOT(Open1()));
// Connecting multiple signals to a slot
QObject::connect(ui.btn_Open, SIGNAL(clicked()), this, SLOT(Open1()));
QObject::connect(ui.btn_ClickBool, SIGNAL(clicked()), this, SLOT(Open1()));
// Connecting a signal to another signal
QObject::connect(ui.btn_Open, SIGNAL(clicked()), ui.btn_ClickBool, SLOT(click()));
// Disconnecting all connections
QObject::disconnect(ui.btn_Open, 0, 0, 0);
// or
ui.btn_Open->disconnect();
// Disconnecting a specific connection
QObject::disconnect(ui.btn_Open, SIGNAL(clicked()), 0, 0);
// or
ui.btn_Open->disconnect(SIGNAL(clicked()));
// Disconnecting connecting objects
QObject::disconnect(ui.btn_Open, 0, ui.btn_ClickBool, 0);
// or
ui.btn_Open->disconnect(ui.btn_ClickBool);
By following these guidelines and best practices, you can effectively use Qt signals and slots to create robust and scalable applications.