Is it possible to auto connect all object's signals to another object?
-
Maybe the question is stupid, and the method which I search for is unuseful, but...
I want a TheMostImportantClass instance to be informed about all signals emitted by all instances of SomeOtherClass. I know only one way to do it: to connect every SomeOtherClass's signal to TheMostImportantClass's slots manually in the constructor. But it's too boring. :-(
Is there another way?
-
How do you want to connect them to the slots? Do you have a 1-to-1 mapping of signals to slots in your classes? Do you want all signals connected to a single slot or something in between?
-
bq. How do you want to connect them to the slots?
I want to have something like the "Automatic Connections":http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html#automatic-connections feature in uic: signals should be automatically connected with specially named slots of TheMostImportantClass.
-
It's not a feature of uic. It's a feature of QMetaObject :-)
so you can use it in any QObject.see "here":http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html#widgets-and-dialogs-with-auto-connect
@
class myClass : public QObject
{
...protected slots:
void on_connectObj_signal1();
void on_connectObj_signal2();
void on_connectObj2_signal1();
void on_connectObj2_signal2();private:
ConnectObject* m_connectObj;
ConnectObject2* m_connectObj2;
};myClass::myClass()
{
m_connectObj = new ConnectObject; // which class ever :-)
m_connectObj2 = new ConnectObject2; // which class ever :-)
QMetaObject::connectSlotsByName(this);
}
@when ConnectObject and ConnectObject2 have a signal with signatur
@
void signal1();
void signal2();
@it should be automatically connected.
-
It's not exactly the same that I'm looking for. Here, I must create a slot for every instance of ConnectObject and ConnectObject2. I want to have a slot which is called when a signal is emitted by any of registered ConnectObject's. Something like this:
@class myClass : public QObject
{
...protected slots:
void on_connectObjects_signal1(ConnectObject* sender);
void on_connectObjects_signal2(ConnectObject* sender);private:
QList<ConnectObject*> m_connectObjects;
};@I feel that it's simpler to make this myself. :-(
-
You can look at "QMetaObject":http://doc.qt.nokia.com/4.7/qmetaobject.html and use methodCount() and method() to inspect the slots and connect them to another object.
-
As Volker said use QMetaObject and to make it simpler you could wrap up you object creation inside a factory class method that creates an object and then uses Volker's suggestion to iterate and make all the connections.
-
Or... simply make the connections from the TheMostImportantClass constructor, if you have control over that.
If all your instances of TheMostImportantClass have a limited number of parents (preferably all the same one), you might also use an event filter on those parents to look for the QChildEvent. If a child of type TheMostImportantClass is added, you can hook up your connections from there. That would allow you to do this without touching any other code in your application and without the need for a factory method. Note that if your TheMostImportantClass has a 0 parent, this approach will fail miserably.
-
Yes that is a good approach too. It depends on the relationship of your objects as to which is more suitable. Anyway you have a few options to try now. :-)