Allowing a class to inherit all my other classes
-
Hey there
I am wondering if its possible to have a class that inherits all my other classes. imagine I have classes A, B, C, D. class A is the parent of B and C. class D is not the child of any classes, however I want class D to be accessible to all my other classes (A,B, and C) so that I can call a signal in A,B,C and have the corresponding slot be called in D and vice versa. Is this possible? Am I correct in saying that class D would have to inherit classes A,B, and C? If I do set it up this way, does that mean that in instance of class D would be accessible from classes A,B,C?
Thanks! -
Hi,
[quote]I am wondering if its possible to have a class that inherits all my other classes[/quote]A QObject can only inherit one other QObject (although it can inherit multiple non-QObjects).
But anyway, inheritance is not designed for the use case you've described (see http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance ).
All you need to do is make the slots public. You can then connect the signals from A/B/C to the slots in D, and vice versa.
@
class A : public QObject {
...
signals:
void sig_A();
}class D : public QObject {
...
public slots:
void slot_D();
}
@@
A* a = new A;
D* d = new D;QObject::connect(a, SIGNAL(sig_A()),
d, SLOT(slot_D()));
@In a nutshell, signal-slot connections need neither a parent-child relationship nor a superclass-subclass relationship.
-
So if I make all the slots in class D public, I can then call them from classes A,B,C regardless of parent-child relationships?
-
[quote author="nicky j" date="1392775543"]So if I make all the slots in class D public, I can then call them from classes A,B,C regardless of parent-child relationships?[/quote]Correct.
-
What about calling a specific instance? Say class A creates an instance of class D called Dinst. Can slots in Dinst be called from classes A,B,C (B and C are children of A). Also is it possible to create an instance of a class without that instance being a child? so have class A create and instance of class D but not have the instance of D be a child of class A.
-
[quote]What about calling a specific instance?[/quote]I'm not quite sure what you mean. All connections are made between specific instances.
The main point I have been highlighting in recent posts (in this thread and others) is this:
If you can obtain the pointers to any 2 QObject instances, then you can connect their signals and public slots. It doesn't matter who created the instances, what relationships they have with each other, or even which threads they live in.
This might sound like a dumb question, but it's a genuine and important one: Do you know what pointers are?
-
My understanding is that a pointer 'points' to a piece of datas memory address. I'm probably wrong though
-
[quote author="nicky j" date="1392869734"]My understanding is that a pointer 'points' to a piece of datas memory address. I'm probably wrong though[/quote]That's correct. So each (valid) QObject pointer points to an instance of a QObject.
Were you able to connect your unrelated objects using their pointers?
-
Yes I was! thanks!