the signal and slot mechanism is the core mechanism of QT . Signals and slots are an advanced interface that is applied to communication between objects, which is the core feature of Qt and is also an important part of QT's differentiation from other toolkits. Signals and slots are a communication mechanism defined by QT itself, independent of the standard C/s + + language, so proper processing of signals and slots requires the use of a QT tool called the MOC (Meta Object Compiler) , which is a C + + preprocessor automatically generates the additional code needed for high-level event handling.
In Qt, theConnect () function is a function of the action and execution of the associated part.
setting in the Startup function is not set in the signal slot function. That is, when a start is connected, if placed in the signal slot, then the first action, the association, the second time to implement the function. Also, the slot function in the slot () in the Connect () function must be placed in the public slot or private slot function in the header file definition. Otherwise, the compilation error cannot be achieved. Signals and slots can carry any number and any type of parameter (type to match), they are completely safe in type and do not produce core dumps like callback functions. All classes derived from Qobject or its subclasses (for example, qwidget) can contain signals and slots. When an object changes its state, the signal is emitted by the object (emit), and it does not know who is receiving the signal at the other end. This is the real message encapsulation, which ensures that the object is used as a real software component. Slots are used to receive signals, but they are normal object member functions. A slot does not know if there is any signal connected to itself. Moreover, the object is not aware of the specific communication mechanism
Groove (Slots):
Slots are normal C + + member functions that can be called normally , and their only peculiarity 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.
Since slots are normal member functions, they have access as well as other functions. Access to slots determines who can be associated with them. Like normal C + + member functions, slot functions are divided into three types, public slots, private slots, and protected slots. Same function as C + + three permissions;;
- Public slots: A slot declared in this area means that any object can connect the signal to it. This is useful for component programming, where you can create objects that are not known to each other and connect their signals to slots so that the information can be passed correctly.
- Protected slots: A slot declared in this area means that the current class and its subclasses can connect the signal to it. This applies to those slots, which are part of the class implementation, but whose interface interfaces are externally oriented.
- Private slots: A slot declared in this area means that only the class itself can connect the signal to it. This applies to very tightly connected classes.
slots can also be declared as virtual functions , which is also very useful. be inherited to use;;
Signal (signals):
When a signal changes the internal state of its customer or owner, the signal is emitted by an object (emit). Only classes that have defined this signal and their derived classes can emit this signal. When a signal is emitted, the slot associated with it will be executed immediately, just like a normal function call. The signal-slot mechanism is completely independent of any GUI event loop. Only if all the slots return after the transmit function (emit) is returned. If there are multiple slots associated with a signal, then when the signal is emitted, the slots will be executed one after the other, but the order in which they are executed will be random and indeterminate, and we cannot artificially specify which first to execute and which to execute.
The signal is declared in the header file, and QT's signals keyword indicates that it has entered the signal declaration area and can then declare its own signal.
The signal is associated with the slot Connect () function:
The signal of an object is associated with the slot function of another object by calling the Connect function of the Qobject object, so that when the emitter emits the signal, the receiver's slot function is called
In general, use two types of formats:
1, qmetaobject::connection qobject::connect (const QOBJECT * sender, const char * signal, const QOBJECT * Receiver,const CH Ar * method, Qt::connectiontype type = qt::autoconnection) [Static]
Qlabel *label = new Qlabel; Qscrollbar *scrollbar = new Qscrollbar; Qobject::connect (ScrollBar, SIGNAL (valuechanged (int)), label, SLOT (Setnum (int))); Use SIGNAL () with SLOT () macros;; 2. Qmetaobject::connection qobject::connect (const qobject * sender, pointertomemberfunction signal,const QObject * Receiver, pointertomemberfunction method, Qt::connectiontype type = qt::autoconnection) [static] Qlabel *label = new QL Abel Qlineedit *lineedit = new Qlineedit; Qobject::connect (LineEdit, &qlineedit::textchanged,label, &qlabel::settext); Using instance addresses for reference signals and slots
We can use the Disconnect function to disconnect the signal when it is not necessary to continue to associate with the slot. Defined as follows:
BOOL Const Const Char * signal, constconstChar * member) [Static]
1, Disconnect everything connected to an object ' s signals:
Disconnect (myObject, 0, 0, 0);
equals Myobject->disconnect ();
2, Disconnect everything connected to a specific signal:
Disconnect (MyObject, SIGNAL (Mysignal ()), 0, 0);
equals Myobject->disconnect (SIGNAL (Mysignal ()));
3, Disconnect a specific receiver:
Disconnect (myObject, 0, myreceiver, 0);
equals Myobject->disconnect (Myreceiver);
0 may be used as a wildcard, meaning ' any signal ', ' any receiving object ', or ' any slot in the receiving object ', Respecti Vely.
The sender may never is 0. (You cannot disconnect signals to more than one object in a single call.)
If signal is 0, it disconnects receiver and method from any signal. If not, only the specified signal is disconnected.
If Receiver is 0, it disconnects anything connected to signal. If not, the slots in objects and other than receiver is not disconnected.
If method is 0, it disconnects anything this is connected to receiver. If not, only slots named method would be disconnected, and the other slots is left alone. The method must are 0 if receiver is left out, so you cannot disconnect a specifically-named slot on all objects.
Qt Connect Signal Slot