C++ object composition and signals and slots
I am having some issues using signals and slots with object composition
I have two objects ObjectA and ObjectB
ObjectA has a member of type ObjectB. Both objects inherit from QObject.
ObjectA and ObjectB has both signals and slots. When I attempt to connect the signals or slots in ObjectB using
QObject::connect(this,SIGNAL(functionSignal(int,int)), ObjectA.ObjectB, SLOT(ObSlot(int,int)) ) );
QObject::connect(ObjectA.ObjectB,SIGNAL(ObSignal(int,int)),),this, SLOT(functionSlot(int,int)) ) );
Then QT doesn’t seem to recognizing any of the signals or slots contained in ObjectB…..
Is there something I am missing here?
How does QT handle connecting signals and slots when using object composition? Is there something special I need to do to get this going?
does the MOC compiler know about signals and slot from objects that are composed members of other objects?
when I preview this post in the preview windows it seems to be removing the argument inside the SIGNAL or SLOT macros... not sure why this is happening..
[andreyc EDIT]: Added @ around the code.
Is ObjectB a pointer?
Are there any warnings or errors during compilation or execution of your app?
Could you post how do you declare and instantiate ObjectB.
[quote]when I preview this post in the preview windows it seems to be removing the argument inside the SIGNAL or SLOT macros… not sure why this is happening.. [/quote]
You need to put @ characters around your code to make it looks nice.
Ok so i believe I answered my own question so I thought i should post here to explain.
What I noticed is that it doesn't seem like I can connect signals and slots when the object that contains the signals or slots are instaiated on the stack. Meaning that if they are not allocated as pointer to objects created with the "new" operator on the heap. For example
This will work
ClassA *classA_inst = new ClassA();
ClassC *classC_inst = new ClassC();
But if I do this:
This wont works.
I am sure this was obvious to most here, but i was not aware that you cannot using signals and slots when the object was created on the stack....
Is this correct? I am I sane?
If this is correct why is this? why do we have to using the heap to create the objects for using signals and slots?
Please use code tags for code.
Of course you can use stack based variables. Look at the connect() signature. It takes a pointer so in case of a stack based variable you need an address of it:
X* ptr = ...
X nonptr = ...
That's basic c++ ;)
And another "basic C++" to remember that as soon as control flow leaves a function all stack based variables are destroyed.
So a signal will never be fired or a slot will never be called or an app will get an exception.