Patrick Z
Patrick Z

Reputation: 303

Static Signals in QT

Im looking for the elegant pattern to follow the events of all instances of a class. So in theory it should be possible to emit a signal as from the class either directly or within a static method (which doesn't seem to be necessary then) because:

The connect() method declares a sender and the signature of it. In this case it could be the class. There is no specific expectation to the sender. As far as I understood, the signal itself is an object on the event stack, oriented to the sender. It shouldn't matter where is was generated(in a static or non static context/method).

So in general: what might be the issue with declaring the signal a static function other that expect the object signalling it is a valid Instance of the class.

It is already possible now through a helper-instance (e.g. the first instance) of the same class (referable through a static public pointer within the class) or for clarification of a specific helper class instance. Then one has to connect to that static pointer and through that, listen to all of them. Every Instance has then indirectly emit through it by

helperInstance->staticEmit(signalFoo,this);

So the question is: is there a more direct/elegant way or should QT look into this?

Upvotes: 0

Views: 255

Answers (1)

David van rijn
David van rijn

Reputation: 2220

In theory, Qt Could implement this feature, by basically adding code generation for the helper object code you showed to the moc compiler.

Because any signal is a function declaration, of which the implementation is generated by the moc compiler. This implementation is basically just a:

for( auto& slot: connected_slots):
    slot.object->slot.function()

(very simplified)

this connected_slots needs to live somewhere. ie. it is a variable, which needs to be stored in memory. So in any case you are going to need an object of some kind to store this in. This is normally in the QObject base class.

My guess for the reason that they didn't add this, is because the Qt framework is very object oriented, and the added complexity outweighs the benefits.

Upvotes: 2

Related Questions