Quite a frequent problem when working with signals with slots in Qt5, according to my observations on the forum, is the connection of slots in the syntax on the pointers to signals having an overload of the signature. The same applies to slots that have an overload.
Let's take a test class that has overloaded signals.
In this tutorial we will learn How to use signal and slots in qt. File-New File or Project Applications-Qt Gui Application-Choose We keep the class as MainWindow as given by default. The need for the dynamic addition of signals and slots arises when binding a script interpreter, such as Qt Script for Applications (QSA), to the C code in your application. In QSA, it is possible to use Qt's meta-object system to communicate between objects created in the C code and objects created in a script. It would be possible to have the slots to which the resized and moved signals are connected check the new position or size of the circle and respond accordingly, but it's more convenient and requires less knowledge of circles by the slot functions if the signal that is sent can include that information.
Here there is a signal, with an overload of the signature. Connect this signal will also be to the slots that are declared in the Widget class, and which also have an overload of the signature.
Within Qt4, everything was solved quite simply by specifying the signature of the signal and the slot in the SIGNAL and SLOT macros.
But in Qt5, when writing in the new syntax of signals and slots, there are some problems. Because you need to make the static_cast of the method signature.
By the way, the new syntax also allows you to connect signals to slots with a smaller signature, as it was in Qt4.
And now a stumbling block. Why use the new syntax of signals and slots? I still hear this question from time to time. Especially when people see such terrible castes of signatures.
In general, for me this is enough, but for you?
Trolltech Documentation Qt Quarterly |
Dynamic Signals and Slots |
by Eskil Abrahamsen Blomfeldt |
Signals and slots are declared at compile-time, and normally youcannot add new signals and slots to a meta-object at run-time. Insome situations, it is useful to extend a meta-object while anapplication is running to obtain truly dynamic functioninvocation and introspection.
The need for the dynamic addition of signals and slots arises when bindinga script interpreter, such as Qt Script for Applications (QSA), to theC++ code in your application. In QSA, it is possible to use Qt'smeta-object system to communicate between objects created in theC++ code and objects created in a script. Qt 4's meta-object systemmakes this sort of extension possible, with a little bit of hackery.
This article will get you started writing a dynamic signals and slotsbinding layer for Qt 4. We will review an abstract base class,DynamicQObject, that offers the required functionality.The 'mini-framework' presented here can serve as afoundation for doing more advanced extensions of Qt's meta-objectsystem.
Expanding the Q_OBJECT Macro |
Any QObject subclass that declares signals andslots must contain the Q_OBJECT macroin its definition. This is a magic macro that expands to a set of privatedeclarations:
Q_OBJECT is 'magic' becauseqmake and mocwill recognize it and automagically generate code for the class's meta-objectduring compilation of the project.
We want to write a DynamicQObject class that mimics part of thebehavior of the moc but that does not limit us to staticallydeclared signals and slots. Since we need to prevent the moc fromcompiling our class, we can't use theQ_OBJECT macro; instead, wewill copy its definition and insert it into our code.
The Q_OBJECT macro is defined insrc/corelib/kernel/qobjectdefs.h (which is included by<QObject>). It declares a number of functions that must bereimplemented to fully support Qt's meta-object system. For thepurposes of this article, it will be sufficient to reimplement just oneof these functions:
When a signal is emitted, Qt uses qt_metacall() to invoke theslots connected to the signal. The first parameter, call, is thenset to QMetaObject::InvokeMetaMethod.(The qt_metacall() function is also used for other types of accessto the meta-object, such as setting or getting properties.)
The second parameter is an index that uniquely identifies a signal orslot in the hierarchy of classes inherited by the current object. Thelast parameter is an array that contains pointers to arguments,preceded by a pointer to the location where the return value shouldbe placed (if any).
The DynamicQObject Class Definition |
Here's the definition of the DynamicQObject class:
The intended usage pattern of DynamicQObject involves subclassingit and adding some functionality. Whenever we add a slot to anobject, we would expect this slot to execute code when it is called.Since this part of the mechanism is dependent on the language towhich we want to bind, it must be implemented in a subclass.
The pure virtual function createSlot() returns an instance ofan appropriate subclass of the abstract class DynamicSlot thathandles function invocation for your specific language binding.DynamicSlot is defined as follows:
DynamicSlot subclasses must reimplement the call() functionto handle a slot invocation. The arguments array's first entry isa pointer to a location where we can put the return value. Thispointer is null if the signal's return type is void.
The rest of the array contains pointers to arguments passed to theslot, in order of declaration. We must cast these pointers tothe correct data types before we can use them. For example, if theslot expects a parameter of type int as its first parameter, thefollowing code would be safe:
Implementation of connectDynamicSlot() |
The connectDynamicSlot() function connects a static signaldeclared in an arbitrary QObject instance toa dynamic slot in the current DynamicQObject instance. The functiontakes a pointer to the sender object (the receiver is always this)and the signatures of the signal and slot. The function returns falseon error.
The first step is to normalize the signatures passed to the function.This will remove meaningless whitespace in the signatures, making itpossible for us to recognize them. Then, we check whether thesignatures are compatible, to avoid crashes later.
If the argument lists are compatible for the two functions, wecan start resolving the signal and slot indexes. The signal isresolved using introspection into the providedQObject object. If the signal was not found,we return false.
Resolving the (dynamic) slot is a bit more work:
We check whether the slot signature has been registered before in thesame DynamicQObject instance. If it hasn't, we add it to our listof dynamic slots.
We use aQHash<QByteArray, int>called slotIndices and aQList<DynamicSlot *> calledslotList to store the information needed for the dynamic slots.
Finally, we register the connection with Qt's meta-object system.When we notify Qt about the connection, we must add the number ofmethods inherited from QObject to our slotId.
The connectDynamicSignal() function is very similar toconnectDynamicSlot(), so we won't review it here.
Implementation of qt_metacall() |
Qt's meta-object system uses the qt_metacall() function to accessthe meta-information for a particular QObjectobject (its signals, slots, properties, etc.).
Since the call may indicate access to the meta-object of theQObject base class, we must start by callingQObject's implementation of the function.This call will return a new identifier for the slot, indexing the methods inthe upper part of the current object's class hierarchy (the current classand its subclasses).
If the QObject::qt_metacall() callreturns -1, this means that the metacall has been handled byQObject and that there is nothing to do.In that case, we return immediately. Similarly, if the metacall isn't a slotinvocation, we follow QObject's conventionand return an identifier that can be handled by a subclass.
In the case of a slot invocation, the identifier must be a valididentifier for the DynamicQObject object. We don't allowsubclasses to declare their own signals and slots.
If all goes well, we invoke the specified slot (usingDynamicSlot::call()) and return -1 to indicate that themetacall has been processed.
Implementation of emitDynamicSignal() |
The last function we need to review is emitDynamicSignal(), whichtakes a dynamic signal signature and an array of arguments. The functionnormalizes the signature then emits the signal, invoking any slotsconnected to it.
The arguments array can be assumed to contain valid pointers. A simpleexample of how to do this can be found in the accompanying example'ssource code, but as a general rule,QMetaType::construct() isyour friend when you are doing more advanced bindings.
If the (dynamic) signal exists (i.e., it has been connected to aslot), the function simply usesQMetaObject::activate() to tellQt's meta-object system that a signal is being emitted. It returnstrue if the signal has previously been connected to a slot;otherwise it returns false.
Pitfalls and Pointers |
Having explained the architecture of this simple framework, I'd liketo conclude by explaining what the framework doesn't do and howyou can expand on it or improve it to suit your needs.
Copyright © 2006 Trolltech | Trademarks |