QT is a cross-platform C + + GUI application architecture, which provides a rich set of widgets, object-oriented, easy to extend, real component programming and so on, even more noticeable is that the most popular KDE desktop environment on Linux is built on the basis of the QT library. QT supports the following platforms: ms/windows-95, 98, NT and 2000;unix/x11-linux, Sun Solaris, HP-UX, Digital UNIX, IBM AIX, SGI irix;embedded-support Framebuffer's Linux platform. With the rapid development and popularization of KDE, QT is likely to become the preferred GUI for software development on Linux Windows platforms.
Signal and slot mechanism is the core mechanism of QT, to be proficient in QT programming must know the signal and the slot. 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), a C + + preprocessor that handles high-level events from Additional code that is required to generate a dynamic build.
In many of the GUI toolkits we know, widgets have a callback function that responds to each action they can trigger, which is usually a pointer to a function. However, the signals and slots in QT replace these messy function pointers, making it more concise and straightforward to write these communication programs. Signals and slots can carry any number and any type of parameter, they are completely safe in type and do not produce a core dumps like a callback function.
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), which is all that the object has to do, 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. Furthermore, the object is not aware of specific communication mechanisms.
You can connect a lot of signals to a single slot, or you can connect a single signal to a lot of slots, or even connect one signal to another, and it's possible to send a second message regardless of when the first signal is fired. In short, signals and slots construct a powerful component programming mechanism.
When a signal changes the internal state of its customer or owner, the signal is emitted by an object. 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. For example, three signals are defined below: [CPP] view plain copy print? Signals:void mysignal (); void mysignal (int x); void Mysignalparam (int x,int y);
In the definition above, signals is the key word for QT, not C + +. The next line of void Mysignal () defines the signal mysignal, which does not carry parameters, and the next line of void mysignal (int x) defines the mysignal of the same name, but it carries an shaping parameter, which is somewhat similar to the virtual letter in C + + Number. The formal signal declaration is the same as the normal C + + function, but the signal does not have a function body definition, in addition, the return type of the signal is void, do not expect to be able to return any useful information from the signal.
The signals are generated automatically by the MOC and they should not be implemented in the. cpp file.
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. 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.
The declaration of the slot is also made in the header file. For example, three slots are declared below: [CPP] view plain copy print? Public slots:void Myslot (); void Myslot (int x); void Mysignalparam (int x,int y);
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 the receiver's slot function is called when the emitter emits the signal. The function is defined as follows: [CPP] view plain copy print? BOOL Qobject::connect (const QOBJECT * sender, const char * signal, const QOBJECT * receiver, const char * mem ber) [Static]
The purpose of this function is to associate the signal signal in the emitter sender object with the member slot function in receiver receiver. When specifying the signal signal, you must use the QT macro signal (), which must be used when specifying the slot function (). If the emitter and receiver belong to the same object, then the receiver parameter can be omitted from the connect call.
For example, the following defines two objects: the Label object label and the ScrollBar object scroll, and the valuechanged () signal is associated with the Setnum () of the Label object, and the signal also carries an shaping parameter so that the label always displays the value of the position where the scroll bar is located. [CPP] view plain copy print? Qlabel *label = new Qlabel; Qscrollbar *scroll = new Qscrollbar; Qobject::connect (scroll, SIGNAL (valuechanged (int)), label, SLOT (Setnum (int)));
One signal can even be associated with another signal, see the following example: [CPP] view plain copy print? Class Mywidget:public Qwidget {Public:mywidget (); ... signals:void