Can I access Qt signals / object slots out of scope?

Do the Qt signals / slots correspond to the C ++ region?

Say I have the following classes: Home, Kitchen, Basement, Stove and Shelf.

class House {Kitchen kitchen, Cellar cellar;}; class Kitchen {Stove stove;}; class Cellar {Shelf shelf;}; 

Now I want to send a signal from the shelf to the basement to the stove in the kitchen. Is this the only way to do this by connecting the signal from the shelf to the basement and the slot from the kitchen to the stove, and then in the house connecting the basement and the kitchen? Or is there a way to do this directly?

I have a class that should interact with the user interface, and I wonder if I need to β€œproxy” all the different signals / slots through intermediate classes. Or is it an indication of poor design?

+4
source share
3 answers

You can connect to any method at home, as there you can access both objects. A β€œconnector” must have access to both the sender and receiver at compile time so that everything is in it.

+3
source

You should be able to simply link the signal from the Shelf instance to the Stove instance.

in home,

connect(cellar->shelf,SIGNAL(signalHere()),kitchen->stove,SLOT(slotHere()));

just make sure the shelf and stove are public variables in Kitchen and Cellar and you will be set

+3
source

You cannot use signals / slots for classes that are not QObjects, so no, your example will not work at all.

You can bypass encapsulation if you initialize children with your parent, so you can do dirty tricks, for example: connect(this->shelf, SIGNAL(signalHere()), kitchen->children()[0], SLOT(aStoveSlot())) . however, this will only work if the first child of the kitchen really is the Stove ... so since this is an obvious addiction, you must make it visible by making the stove open or by adding a method of access to the stove.

+1
source

All Articles