PDA

View Full Version : Connect Signal/Slot to any instance of another class



squeegedog
8th February 2014, 06:42
There's probably an answer for this somewhere out there, but its a difficult search to word that doesn't return 100 million results of basic signal/slot questions.

I have a single instance of a class that owns a countdown timer, when the timer reaches 0, I would like it to emit a signal that can be received by any instance of another class without having to hard-code each instance. This obviously can be easily performed by writing out the code for each instance, such as:


connect(objA, SIGNAL(timeup()), objB1, SLOT(countup()));
connect(objA, SIGNAL(timeup()), objB2, SLOT(countup()));


But when there gets to be 10 instances of objB, gets to be a little much, especially when that 10 today could be 20 by the end of the project. Was hoping for a way to just say


connect(objA, SIGNAL(timeup()), &objB, SLOT(countup()));

where objB is the defined name of the class before any instances of it are invoked.

Thanks!

squeegedog
8th February 2014, 09:38
Meh, thought maybe I had come up with a solution, but Qt being a real pain, so I can't tell if this will work or not.

I'm now trying to create the connect statement in the constructor of my objB and passing in a pointer to objA, but the connect statement is throwing errors:


error: no matching function for call to 'QObject::connect(QWidget*&, const char*, objB* const, const char*)'
candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)

Both non-matching items have Q_OBJECT instruction and I can't for the life of me figure out why its showing as QWidget*& since I'm passing in a pointer (using "this")

edit: Thought i should add, objA is derived QGraphicsView, objB is derived QGraphicsItem

Added after 42 minutes:

Close, delete or whatever this thread.

QGraphicsItem does not inheric QObject. Using QGraphicsObject inherits QGraphicsItem and QObject.

Thus placing the connect signal in the constructor allows any instance of the class to use signal/slot connection