Translator Note: QT is a set of cross-platform C GUI applications frameworks, recently very popular. Its signal is very different from the Signals and Slots mechanisms and the message mechanism of Windows.
Signals and slots
Signals and slots are used for Communication Between Objects. The signal feature of qt and probably the part difference. Signal and slots are used to inter-object communication. Signal / slot mechanism is an important feature of Qt, which may be the biggest difference from other toolkits. In GUI programming we often want a change in one widget to be notified to another widget. More generally, we want objects of any kind to be able to communicate with one another. For example if we were parsing an XML file we might want to notify A List View That We're Using To Repesent The XML File's StRUCTURE WHENEVER WE ENCOUNTER A New Tag. In graphical user interface programming, we often want a window to change the notification of other windows. More universally, we want any type of object to communicate with other objects. For example, when we parsing the XML file, we may wish to issue a notification to a list of structures we use to display the XML file when encountering a new tag. Older toolkits achieve this kind of communication using callbacks. A callback is a pointer to a function, so if you want a processing function to notify you about some event you pass a pointer to another function (the callback) to the processing function. The processing function then calls the callback when appropriate. Callbacks have two fundamental flaws. Firstly they are not type safe. We can never be certain that the processing function will call the callback with the correct arguments. Secondly the callback is strongly coupled to the processing function since The Processing Function Must Know Which Callback to Call. The old kit uses a callback to achieve this. The callback is a pointer to the function, so if you want a handler to inform you that some events have occurred, you can pass a pointer (callback) pointing to other functions to the handler. The processing function calls the callback function when appropriate. There are two basic disadvantages in callback. First, it is not type safe. We cannot guarantee that the handler will call with the correct parameters. Second, the callback and processing function are strongly coupled because the processing function must know which callback is called.
Old-fashioned callbacks impair component programming In Qt we have an alternative to the callback technique. We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many pre-defined signals, but we can always subclass to add our own. A slot is a function that is called in reponse to a particular signal. Qt's widgets have many pre-defined slots, but it is common practice to add your own slots so that you can handle the signals that you are interested in. In QT, we have additional choices in removal technology. We use signals and slots. When the special event occurs, the signal is issued. Qt's window has many predefined signals, but we can also add our own. A slot is a function called a particular signal response.
Signals and Slots facilitate true type-safe component programming The signals and slots mechanism is type safe: the signature of a signal must match the signature of the receiving slot (In fact a slot may have a shorter signature than the signal it receives because it. .. can ignore extra arguments) Since the signatures are compatible, the compiler can help us detect type mismatches Signals and slots are loosely coupled: a class which emits a signal neither knows nor cares which slots receive the signal Qt's signals and slots mechanism ensures. . that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time signals and slots can take any number of arguments of any type They are completely typesafe:.! no more callback core dumps signal Slot mechanisms are type security: signal signatures must match the signature of the receiving slot. (In fact, the signature of the slot may be shorter than the signature of the signal it receives because it can ignore additional parameters.) Because the signature is compatible, the compiler can help us detect the type of mismatch. Signal and slots are loosely coupled: a class that makes a signal does not know which slot does not care to receive this signal. Qt's signal and slot mechanism guarantee that if you connect a signal to a slot, the slot will be called in the signal parameter. Signal and slots can carry any, any type of parameters. They are completely type safe, there is no callback, core Dumps! All classes that inherit from QObject or one of its subclasses (eg QWidget) can contain signals and slots. Signals are emitted by objects when they change their state in a way that may be interesting to the outside world. This is all the object does to . communicate it does not know or care whether anything is receiving the signals it emits This is true information encapsulation, and ensures that the object can be used as a software component for all the Qobject or one of its subclasses. (eg: QWidget) The inherited classes contain signals and slots. When the object changes it in some way, the signal will be excited by the object when it is interested in the outside world. This is what all object communications are done. It doesn't know what is not interested in receiving. This is a real information package, ensuring that the object can be used as a software component.
Slots can be used for receiving signals, but they are normal member functions. A slot does not know if it has any signals connected to it. Again, the object does not know about the communication mechanism and can be used as a true software component. Slots can be used to receive signals, but they are ordinary member functions. The slot does not know if there is a signal to connect it. Objects do not know the communication mechanism, can be used as a real software component. You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you desire. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever You can connect arbitrary signals to your desired single slot, and a signal can also be connected to multiple slots you expect. It may even be directly connected to other signals. (This means that when the current signal is excited, another signal will immediately stimulate another signal). For TOGETHER, SIGNALS AND SLOTS Make Up A Powerful Component Programming Mechanism. In short, the signal and slot have established a powerful component programming mechanism.
A Small Example a Minimal C Class Declaration Might Read: A minimum C class can be declared as follows: Class foo {public: foo (); int value () const {return value (} void setValue (int); private: int val; }; A small qt class might read: a small QT class can be like this: class foo: public Qobject {q_object public: foo (); int value () const {return val;} public slots: void setValue (int); signals: void valueChanged (int); private: int val;}; This class has the same internal state, and public methods to access the state, but in addition it has support for component programming using signals and slots: this class can tell the Outside World That Its State Has Changed by Emitting A Signal, ValueChanged (), AND IT HAS A SLOT Which Other Objects May Send Signals To. This class has the same internal state and access state, but it has the utilization signal and Slots for component programming: This class can tell the outside its status by exciting a signal valueChanged (), and it also has a slot that can be sent to other objects. All classes That Contain Signals and / or slots must mention q_object in their declaration. All classes containing signals and / or slots must add Q_Object to their declarations. Slots areware. Here is a Possible Implementation of foo :: setValue (): Slot is implemented by app, this is a possible implementation of foo :: setValue (): void foo :: setValue INT V) {if (v! = VAL) {VAL = V; Emit ValueChanged (V);}} The line emit valuechanged (v) Emits the signal valuechanged from the object. as you can see, you emit a signal by use Emit Signal (ValueChanged (V) This row excited from the object VALUECHANGED. As you can see, you use the EMIT signal (parameter) to excite a signal. Here Is One Way To Connect Two of these Objects Together: A method that connects two such objects together.
Foo A, B; Connect (& A, Signal (INT)), & B, SLOT (SetValue (INT))); B.SetValue (11); // a == undefined B == 11 A.SetValue (79 ); // a == 79 b == 79 B. Value (); calling a.setValue (79) WILL MAKE A EMIT A Valuechanged () Signal, Which B Will Receive In Its SetValue () Slot, IEBSetValue (79 IS Called. B Will Then, in turn A valueChanged () signal, B will receive this signal in its setValue slot, that is, B.SetValue (79) is called. Then B will turn through the same valuechanged () signal, but because there is no slot to be connected to the valueChanged () signal, there is no signal (signal disappearance). Note that the setValue () function sets the value and emits the signal only if v! = Val. This prevents infinite looping in the case of cyclic connections (eg if b.valueChanged () were connected to a.setValue ()). Note The setValue () function assigns valued and excited signals only when V! = VAL. This avoids loop connections (for example, if B.ValueChanged () is also connected to the infinite loop of A.SetValue ()). This Example Illustrates That Objects Can Work TOGETER WITHOUT KNOWING EACH OTHER, AS Long As The IS Someone Around To Set Up A Connection BetWeen THEM INITIAL. This example explains that the object can work together without knowing the other party, as long as they initialize Establish a connection for them. The Preprocessor Changes Or Removes The Signals, Slots and Emit Keywords So That The Compiler Is Presented with Standard C . Preprocessor changes or removes the Signals, Slots, and Emit keywords, so the compiler treats it in standard C .
Class definition Run the moc on class definitions that contain signals or slots. This produces a C source file which should be compiled and linked with the other object files for the application. Run moc (Meta Object Compiler) to process a signal containing or slots . This produces a C source code that can be compiled and connected to other target files in the application. Signals signal Signals are emitted by an object when its internal state has changed in some way that might be interesting to the object's client or owner. Only the class that defines a signal and its subclasses can emit the signal. When the customer or owner of the object The internal state of the object interested is changed, and the signal is excited. Only the types of signals defined and their subclasses can excite the signal. A list box, for example, emits both highlighted () and activated () signals. Most objects will probably only be interested in activated () but some may want to know about which item in the list box is currently highlighted. If the signal is Interesting to Two Different Objects You Just Connect The Signal To Slots In Both Objects. A list box can inspire the heighLighed () signal can also excite the activated () signal. Most objects may only be interested in activated (), but sometimes I want to know which element in the list box is currently highlighted. If the signal is interested in two different objects, you can connect the signal to the slot of the two objects. When a signal is emitted, the slots connected to it are executed immediately, just like a normal function call. The signal / slot mechanism is totally independent of any GUI event loop. The emit will return when all slots have returned. When the signal is excited The slot connected to it will be executed immediately, just like a normal function call. The signal / slot mechanism is completely independent of any GUI event loop. Emit will return when all slots return. If SEVERAL SLOTS Are Connected to One Signal, The Slots Will Be Executed ONE AFTER, WHEN THE SIGNAL IS Emitted. If several slots are connected to the same signal, these slots are excited in signals. The time is executed in any order. Signals Are Automatically Generated by The MOC AND MUST NOT BE. The.E. Use Void. The signal is automatically generated by MOC and does not have to be implemented in .cpp.
They never return values (that is, void). A note about arguments. Our experience shows that signals and slots are more reusable if they do not use special types. If QScrollBar :: valueChanged () were to use a special type such as the hypothetical QRangeControl :: Range, it could only be connected To Slots Designed Specifically for Qrantruontrol. Something as Simple As The Program in Tutorial 5 Would Be Impossible. About parameters. Our experience shows that if the signal and slot do not use a special type, their reusability will be higher. If QscrollBar :: ValueChanged () uses a specified type, assume that it is QrangeControl :: Range, which can only be connected to QrangeControl. Things are as simple as tutorial5. Slots Slot A slot is called when a signal connected to it is emitted Slots are normal C functions and can be called normally;.. Their only special feature is that signals can be connected to them A slot's arguments can not have default values, and, Like Signals, IT IS Rarely Wise To Use Your Own Custom Types for Slot Arguments. Slots will be called when it is excited in chain street. The slot is a normal C function that can be called in ordinary. Since slots are normal member functions with just a little extra spice, they have access rights like ordinary member functions A slot's access right determines who can connect to it:. Because the slot is slightly extended ordinary member functions, they have an ordinary member function access permission. Access slot determines who can connect to it: A public slots: section contains slots that anyone can connect signals to This is very useful for component programming:. You create objects that know nothing about each other, connect their signals and slots so That Information is Passed Correctly, And, Like A Model Railway, Turn It On and Leave It Running. A Public Slots: Segment contains any slots that can be connected. This is very useful for component programming: You create objects that don't know to each other, connect their signals and slots to allow information to be passed correctly, just like railway models, open it, let it run.
A protected slots: section contains slots that this class and its subclasses may connect signals to This is intended for slots that are part of the class' implementation rather than its interface to the rest of the world a protected slots:.. Section contains it Slots themselves and their subclasses can be connected. This is interested in making the slot into a part of the class, not an external interface. A private slots: section contains slots that only the class itself may connect signals to This is intended for very tightly connected classes, where even subclasses are not trusted to get the connections right a private slots:.. Section contains only the class STD. This is interested in connecting classes, or even subclasses can not want to get the correct connection. You can Also Define Slots to Be Virtual, Which We Have Found Quite Useful in Practice. You can also define slots for Virtual, we find that this is especially useful in actual.
The signals and slots mechanism is efficient, but not quite as fast as "real" callbacks. Signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. This is the overhead required to locate the connection object, to safely iterate over all connections (ie checking that subsequent receivers have not been destroyed during the emission) and to marshall any parameters in a generic fashion. While ten non-virtual function calls may sound like a lot, it's much less overhead than any 'new' or 'delete' operation, for example. As soon as you perform A string, Vector or list Operation That Behind The Scene Requires 'NEW' OR 'Delete', The Signals and Slots Overhead Is Only Responsible for a Very Small ProPorts of The Complete Function CALL . Costs The same is true whenever you do a system call in a slot - or indirectly call more than ten functions On an i586-500, you can emit around 2,000,000 signals per second connected to one receiver, or around 1,200,000 per second connected to. TWO receivers. The SIMPLICITY AND FLEXILITY OF THE SIGNALS AND SLOTS MECHANISM IS Well Worth The overhead, Which Your Users Won't even notice. The slot mechanism is efficient, but it cannot be completely compared to "real" callbacks. Signal and slots are slightly slower because it brings more flexibility, although these differences are not critical in real applications. Typically, the signal that excited a connection to some slots is 10 times slower than the direct call recipient, where there is no virtual function call. This is required to locate the connection object, for safe iteration (traversal) all connections (that is, check the next recipient is not damaged during the excitation period). Maybe 10 non-virtual functions sound a lot, but it is far less than 'new' or 'delete' operation.
For example, when you perform a string, vector or list operation, this requires hidden 'new' or 'delete', and the signal and slot only occupies very little part of the very all function call overhead. This is also the same for your system calls you in your slot or indirect calls. On an i586-500, you can excite the signals that have been connected to one recipient per second, or about 1,200,000 signals that have been connected to the two recipients. The simplicity of the signal and the slot mechanism and the overhead of the flexibility is worth it, and even your users will not find. Meta Object Information element object information The meta object compiler (moc) parses the class declaration in a C file and generates C code that initializes the meta object. The meta object contains names of all signal and slot members, as well as pointers to these functions For more information on Qt's Meta Object System, SEE Why Doesn't QT Use Templates for Signals and Slots ?.) Element Object Compiler (MOC) Resolves the class declaration in the C file and generates C code in initialization. The element object contains the names containing all signals and slot members, just like these function pointers. (For more information about Qt Meta-Object System, see Why does not Qt use templates for signals and slots?) The meta object contains additional information such as the object's class name. You can also check if an object inherits a specific class, For example: Element object contains additional information such as class name, you can also check if an object is inherited from a specified class, for example: if (widget-> inherits ("qbutton")) {// Yes, IT IS A Push Button, Radio Button etc.} A Real Example An example here is a simple commented Example (Code Fragments from QlcdNumber.h). This has a simple example (code snippet in QlcDNumber.h). #Include "qframe.h" #include "qbitarray.h"
class QLCDNumber: public QFrameQLCDNumber inherits QObject, which has most of the signal / slot knowledge, via QFrame and QWidget, and # include's the relevant declarations QLCDNumber indirectly inherited Qobject from has the largest signal / slot knowledge through Qframe and Qwidget, and contains. The relevant statement. {Q_OBJECTQ_OBJECT is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "virtual function QButton :: className not defined" you have probably forgotten to run the moc or to include the MOC OUTPUT IN The Link Command. Q_Object The pre-processor extension declaration declares several member functions implemented by the MOC; if you get a row of compilers like this (should be a linker) error "Virtual Function QButton :: ClassName Not Defeed ", Perhaps you forgot to run MOC or forget to include MOC output in the link command. public: QLCDNumber (QWidget * parent = 0, const char * name = 0); QLCDNumber (uint numDigits, QWidget * parent = 0, const char * name = 0); It's not obviously relevant to the moc, but if you inherit QWidget You Almost Certainly Want to Have The Parent and Name Arguments in Your Constructors, And Pass Them THE PARENT CONSTRUCTOR. This has no obvious relationship with MOC, but if you inherit Qwidget and confident that there is a Parent and Name in your constructor Parameters, pass them into the constructor of the parent class. Some Destructors and Member; The Moc Ignores Member Functions. Some destructors and member functions are omitted here; MOC ignores member functions. Signals: void overflow (); QlcDNumber Emits a signal when is asked to show an impossible value. QlcdNumber will arouse a signal when the impossible value is required. If You Don't Care About overflow, or you know, you can ignore the overflow () Signal, IE don't connect it to any slot. If you don't care overflow, or you know that overflow is impossible, You can ignore the overflow () signal, that is, connect it to any slot.
Irror Functions WHEN THE NUMBER OVERFLOWS, SIMPLY CONNECT The Signal To Two Different Slots. Qt Will Call Both (In Arbitrary ORDER). If you want to overflow Call two different error handles, simply connect the signal to two different slots. Qt two will be called (in any order). public slots: void display (int num); void display (double num); void display (const char * str); void setHexMode (); void setDecMode (); void setOctMode (); void setBinMode (); void smallDecimalPoint (bool );.. A slot is a receiving function, used to get information about state changes in other widgets QLCDNumber uses it, as the code above indicates, to set the displayed number Since display () is part of the class' interface with the rest Of The Program, The Slot Is Public. Slot is a received function to obtain information that changes other components (widgets) status. QlcdNumber uses it, just like the code display above, it is used to set the display number. Because Display () is part of the class interface, Slot is public. Several of the example programs connect the newValue signal of a QScrollBar to the display slot, so the LCD number continuously shows the value of the scroll bar. Routine newValue connected to the display signal QscrollBar slot, the LCD values continuously displayed scroll bar . Note that display () is overloaded; Qt will select the appropriate version when you connect a signal to the slot With callbacks, you'd have to find five different names and keep track of the types yourself attention display () is overloaded. When you connect a signal to this slot, QT will choose the appropriate version. When the callback, you have to find 5 different names and track the type. Some Irrelevant Member Functions Have Been Omitted from this Example. Some unrelated member functions are omitted in the example. }; CopyRight © 2001 Trolltech TradeMarks Qt Version 3.0.1