PDA

View Full Version : How get a pointer to the parent widget



franco.amato
7th September 2010, 19:29
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

tbscope
7th September 2010, 19:35
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?

franco.amato
7th September 2010, 19:39
You can walk the object tree.

How can I do that? I would learn something new!


Or you can set a pointer to your main window in your widget like you said.
Thank you.


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.

tbscope
7th September 2010, 19:48
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.


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.


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.

franco.amato
7th September 2010, 19:56
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.


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?

tbscope
7th September 2010, 20:13
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:


myMainWindow::myMainWindow(...) :
QMainWindow(...)
{
connect(ui->widget1, SIGNAL(...), this, SLOT(...));
}

void myMainWindow::slotEnable(bool on)
{
if(on)
...
else
...
}

wysota
7th September 2010, 20:20
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 :)

franco.amato
7th September 2010, 20:39
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 :)

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

wysota
7th September 2010, 21:37
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.

manojmka
8th September 2010, 10:41
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