[SOLVED] Connecting signal and slot between parent and "grandchild"
-
Hello,
I have an application with a main widget (parentWidget). Within my main widget I create a new widget (we can call it childWidget). Within childWidget I create a new widget (call it grandchildWidget). I want to connect a signal from my grandchild to my parent widget. I tried using:
@connect(this, SIGNAL(updateGeneralStatusSignal()), this->parent()->parent(), SLOT(updateGeneralStatusSlot()));@
however, this did not work. I have been able to use this->parent() between the parent and child widget, but it wont allow me to do it between the parent and grandchild.
Any thoughts?
Thanks
-
Hi,
You're doing it the wrong way.
You should emit, if necessary chain the emit in the child widget, and connect the childWidget signal in parentWidget.
i.e.
@
parentWidget.cpp:
connect(childWidget, SIGNAL(updateGeneralStatusSignal()), SLOT(updateGeneralStatusSlot()));childWidget.h:
class ChildWidget : public QObject
{
Q_OBJECTpublic:
signals:
void updateGeneralStatusSignal();
}childWidget.cpp:
connect(grandChild, SIGNAL(updateGeneralStatusSignal()), SIGNAL(updateGeneralStatusSignal()));
@Your grandChild widget should not care about connecting other widgets to him. Otherwise you'll break orthogonality.
It's the object who knows where the signal should go the should the connections.
-
Hi,
IMO, the reason is that, this->parent()->parent() isn't the widget as you expected. You can output the result of
@
this->parent()->parent()->metaObject()->className()
@
BTW, usually it's not good to use this style for widgets in a real application, as it's hard to maintain. You can pass the pointer of the expected widget to your current widget.
-
[quote author="SGaist" date="1373559965"]Hi,
You're doing it the wrong way.
You should emit, if necessary chain the emit in the child widget, and connect the childWidget signal in parentWidget.
i.e.
@
parentWidget.cpp:
connect(childWidget, SIGNAL(updateGeneralStatusSignal()), SLOT(updateGeneralStatusSlot()));childWidget.h:
class ChildWidget : public QObject
{
Q_OBJECTpublic:
signals:
void updateGeneralStatusSignal();
}childWidget.cpp:
connect(grandChild, SIGNAL(updateGeneralStatusSignal()), SIGNAL(updateGeneralStatusSignal()));
@Your grandChild widget should not care about connecting other widgets to him. Otherwise you'll break orthogonality.
It's the object who knows where the signal should go the should the connections.[/quote]
Perfect! This worked. Thanks!