Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Child and parent widget communication.
I would like to know how a child widget can send some signal to its parent widget?
puterk last edited by
You can make the child emit a signal, and connect it to the parent`s slot.
For your signal:
in your child widget`s .h file:
void somesignal(/arguments if any/); @
in your in your child widget`s cpp:
@emit somesignal(/arguments if any/); @
For your slot:
in your parent widget`s .h file:
void someslot(/arguments if any/);@
in your parent widget`s cpp:
@void someslot(/arguments if any/)
@connect (child, SIGNAL(somesignal(/arguments if any/)),
this, SLOT(someslot(/arguments if any/)));@
Thanks for the reply. Yes, you are right, however we could achieve this signal and slot mechanism only when we know we have separate classes for both.
1)- What to do if I don't know the parent widget at compile time?
2)- Means how to get parent of a child widget at run time?
puterk last edited by
I'm not sure what you are trying to do but perhaps you could put the slots and connect the signal to all the possible parent widgets. This is what I'm doing with a dialog that would have a different parent window during runtime.
Maybe you could provide more information and maybe some codes that you have tried so we could understand more.
[quote author="aswal" date="1387354146"]
What to do if I don't know the parent widget at compile time?
Means how to get parent of a child widget at run time?
To get the parent widget use QWidget::parentWidget()
You can test the parent widget's type either with qobject_cast or QObject::inherits()
QWidget* parentWidget = childWidget->parentWidget();
if( parentWidget->inherits("MyClass") )
MyClass* myParent = static_cast<MyClass*>(parentWidget);
MyClass* myParent = qobject_cast<MyClass*>(parentWidget); //returns 0 if could not be cast
if( myParent )
puterk & raven-worx, thanks for the reply.
Here is my question in detail:
1- I have developed a QT tab plugin.
class QDESIGNER_WIDGET_EXPORT QTab : public QTabWidget
2- I can drag it in qt designer and add new tab in it.
3- What I want that if I drag another instance of tab plugin and drop over the earlier instance created in step 2.
4- So on drop event I want to get the tab widget created in step 2 and access its member function,
andre last edited by
You can, but it's not very modular. You'll end up with tight connections between your components.
The general answer here is that at least the child should know nothing of its parent, unless the two are intimately bound. I get the feeling that that is not the case here. So, the child widget should just expose signals (and slots, if needed), and not care who its parent is.
Normally, neither of the parties of a signal-slot connection should care about who it connects to. That is the responsibility of whoever knows about both of these objects at the same time. Often, the two parties in a signal-slot connection will not have a parent-child relationship at all, but will be in different parts of the same form for instance. In such cases, usually the parent form is responsible for creating the connection, as it has access to both widgets.
Often, a parent has knowledge of who its children will be, but obviously not always. However, there must be a place where you do have knowledge of both objects. That is the place where you make the connection.
thanks, however it doesn't solve the issue I am facing :(.