Tag: QT signal slot
Signal and slot are an important part of QT programming. This mechanism can associate the actions of objects without mutual understanding.
Signal and slot Declaration (1)
In QT programming, q_object must be added to all classes that contain signal and slot. The following example shows how to define signal and slot in a class.
Signal definition:
Siganls:
Void mysignal ();
Void mysignal (int x );
Void mysignal (int x, int y );
Signals is the key word of QT, rather than the key word of C/C ++. In addition, the difference between a signal and a general function is that all of its return values are void, and it does not have a function implementation body, and its function body is automatically generated by MOC.
Slot definition:
Public slots:
Void myslot ();
Void myslot (int x );
Different types of slots have different operation permissions, depending on whether the slot is public, protected, or private.
Class EMPLOYEE: Public qobject
{
Q_object
Public:
Employee () {mysalary = 0 ;}
Int salary () const {return mysalary ;}
Public slots:
Void setsalary (INT newsalary );
Signals:
Void salarychanged (INT newsalary );
PRIVATE:
Int mysalary;
};
Signal and slot Statement (2)
Generally, signal is sent using emit in the event processing function. In the following example, signal is sent after the event processing is complete.
Void EMPLOYEE: setsalary (INT newsalary)
{
If (newsalary! = Mysalary)
{
Mysalary = newsalary;
Emit salarychanged (mysalary );
}
}
Note: Only newsalary! = Mysalary.
The slot is similar to a common C ++ member function. They can be virtual functions, overloads, public functions, proteve ve, or private functions ), they can be called like any c ++ member function and can pass any type of parameters. The difference is that a slot function can be connected to a signal. As long as the signal is sent, this slot function will be automatically called. This task is implemented by the connect function.
The connect function syntax is as follows:
Connect (sender, signal (signal), explorer, slot (slot ));
Sender and consumer er are qobject object pointers, while signal and slot are function prototypes without parameters. The functions of signale () and slot () macros are to convert them into strings.
Rules for using signals and slots:
1. One signal can be connected to multiple slots:
Connect (slider, signal (valuechanged (INT), spinbox, slot (setvalue (INT )));
Connect (slider, signal (valuechanged (INT), this, slot (updatestatusbarindicator (INT )));
When the signal is sent, the slot function is called, but the call sequence is random and uncertain.
2. Multiple signals can be connected to one slot
Connect (LCD, signal (overflow (), this, slot (handlematherror ()));
Connect (calculator, signal (divisionbyzero (), this, slot (handlematherror ()));
The slot function is executed when any signal is sent.
3. One signal can be connected to another signal
Connect (lineedit, signal (textchanged (const qstring &), this, signal (UpdateRecord (const qstring &)));
After the first signal is sent, the second signal is also sent. In addition, the signal and the signal connection are the same as the signal and the slot connection.
4. The connection can be deleted.
Disconnect (LCD, signal (overflow (), this, slot (handlematherror ()));
This function is rarely used. After an object is deleted, QT automatically deletes all connections of this object.
5. The signal and slot functions must have the same parameter type so that the signal and slot functions can be connected successfully:
Connect (FTP, signal (rawcommandreply (INT, const qstring &), this, slot (processreply (INT, const qstring &)));
If the number of parameters in the signal is greater than that of the slot function, extra parameters are ignored:
Connect (FTP, signal (rawcommandreply (INT, const qstring &), this, slot (checkerrorcode (INT )));
If the parameter type does not match, or the signal and slot do not exist, QT will give a warning during the running of the debug status. If the parameter name is included in the connection between the signal and the slot, QT will give a warning.
This article is from the "IT technology" blog. For more information, contact the author!
Description and connection of signal and slot