How get a pointer to the parent widget
Hi to all,
in my application I have this widget structure:
QmainWindow -> CentralWidget -> QWidget
The QMainWindow contains a central widget, and this one contains 2 QWidget.
I would call directly a method of QMainWindow from one of the QWidget is possible?
I saw the QWidget::parentWidget() but I don't think it returns a pointer of the QMainWindow.
I would know if it's possible or if I need to pass a pointer into the ctor.
Best Regards,
Franco
Re: How get a pointer to the parent widget
You can walk the object tree. Or you can set a pointer to your main window in your widget like you said.
Is there something specific you want to achieve? Is it possible via signals and slots or events?
Re: How get a pointer to the parent widget
Quote:
Originally Posted by
tbscope
You can walk the object tree.
How can I do that? I would learn something new!
Quote:
Or you can set a pointer to your main window in your widget like you said.
Thank you.
Quote:
Is there something specific you want to achieve? Is it possible via signals and slots or events?
Simply I need to enable/disable an action created in the QMainWindow from the QWidgets.
Re: How get a pointer to the parent widget
Quote:
Originally Posted by
franco.amato
How can I do that? I would learn something new!
Very simple, just keep calling parent() till it returns 0 or if you use the metadata or name of the object and you find what you wanted.
That said, I don't think this is a good idea.
Quote:
Simply I need to enable/disable an action created in the QMainWindow from the QWidgets.
In such cases I always use a signal/slot combo or an event.
Code:
MainWindow -> acts to widget1 signal in a slot
|
+---- Central widget
|
+---- Widget 1 -> sends a signal to the main window
|
+---- Widget 2
connect(widget1, SIGNAL(widget1ActionEnabled(bool)), mainwindow, SLOT(slotWidget1ActionEnabled(bool)));
Or something similar to that. You only need to add a signal to your widget, emit it at the correct time and implement a very simple slot in your main window.
Re: How get a pointer to the parent widget
Quote:
Originally Posted by
tbscope
Very simple, just keep calling parent() till it returns 0 or if you use the metadata or name of the object and you find what you wanted.
That said, I don't think this is a good idea.
In such cases I always use a signal/slot combo or an event.
Code:
MainWindow -> acts to widget1 signal in a slot
|
+---- Central widget
|
+---- Widget 1 -> sends a signal to the main window
|
+---- Widget 2
connect(widget1, SIGNAL(widget1ActionEnabled(bool)), mainwindow, SLOT(slotWidget1ActionEnabled(bool)));
Or something similar to that. You only need to add a signal to your widget, emit it at the correct time and implement a very simple slot in your main window.
But to do that both classes have to know about each other or how can I create the signal slot? If so my problem is not solved I still have to pass a QMainWindow's pointer through the tree right?
Re: How get a pointer to the parent widget
Explain how you build the UI. Using designer?
I suggest you use Qt Creator, then create a new main window with classes.
Then you can do something like this:
Code:
myMainWindow::myMainWindow(...) :
{
connect(ui->widget1, SIGNAL(...), this, SLOT(...));
}
void myMainWindow::slotEnable(bool on)
{
if(on)
...
else
...
}
Re: How get a pointer to the parent widget
I think franco's "problem" is that QWidget::parentWidget() returns a pointer to QWidget and he doesn't know how to make it a pointer to QMainWindow. Another one of those C++ problems :)
Re: How get a pointer to the parent widget
Quote:
Originally Posted by
wysota
Dear Wysota sorry but you're wrong ( I perfectly know what a cast is ). You are used to underestimate me.
tbscope the problem is that QMainWindow doesn't see directly the QWidget.
The QWidgets are created in the centralWidget class that's part of the QMainWindow class.
I use Visual studio.
Thank you.
Franco
Re: How get a pointer to the parent widget
Quote:
Originally Posted by
franco.amato
Dear Wysota sorry but you're wrong ( I perfectly know what a cast is ). You are used to underestimate me.
tbscope the problem is that QMainWindow doesn't see directly the QWidget.
The QWidgets are created in the centralWidget class that's part of the QMainWindow class.
I use Visual studio.
Thank you.
Franco
So QWidget::parentWidget() doesn't work for you? I don't really see the problem... If your widget is a child of a central widget of the main window then obviously:
1. main window is the grand parent of your widget (parentWidget()->parentWidget())
2. main window is a window of your widget (this->window())
Choose either of the two approaches.
I do not underestimate you, it's just sometimes it is hard to see what you actually have a problem with.
Re: How get a pointer to the parent widget
Hi franco,
It seems your main window knows both the widgets, so you can simply connect some signal from widgets in main window and act upon them in a slot created in Main window.
If you want to do it otherway round, you should definitely have access to Main window from widgets and for that you either need to pass it or you need to find it walking through parentWidget tree or it should be globally accessible.
Manoj