View Full Version : parent() heirarchy misunderstanding?

16th October 2007, 17:20
Hello all... I've been getting confused on the parent() heirarchy. Here's what I've got:

formMainWindow, parent of QMdiArea, parent of QMdiSubWindow, parent of subWidget

I can use this code to initialize (from within the formMainWindow):

QMdiArea * ws = new QMdiArea(this);
subWidget * w = new subWidget;
QMdiSubWindow * sw = ws->addSubWindow(w); // Should set w->parent() to sw

I *think* this works all right... but traversing the heirarchy is another matter. From within subWidget:
QMdiSubWindow * sw = dynamic_cast<QMdiSubWindow *>(parent()); // Returns a valid pointer
QMdiArea * ws = dynamic_cast<QMdiArea *>(sw->parent()); // Returns NULL
formMainWindow * mw = dynamic_cast<formMainWindow *>(sw->parent()); // Also returns NULLSo... what is the parent of a QMdiSubWindow? How can I traverse the heirarchy back to the formMainWindow?

16th October 2007, 17:44
The parent of all subwindows in a mdi area is QMdiArea::viewport(). This is inherited from abstract scroll area.
The viewport is a QWidget so you should write the cast accordingly.

16th October 2007, 19:18
Thanks, I was able to replace that with a simple
static_cast<formMainWindow *>(parent()->parent()->parent()->parent())->subWindowDestroyed(this);within the closeEvent(QCloseEvent *) of the subwindow (to clean up before the thing is actually destroyed).