Results 1 to 3 of 3

Thread: parent() heirarchy misunderstanding?

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default parent() heirarchy misunderstanding?

    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):
    Qt Code:
    1. QMdiArea * ws = new QMdiArea(this);
    2. subWidget * w = new subWidget;
    3. QMdiSubWindow * sw = ws->addSubWindow(w); // Should set w->parent() to sw
    To copy to clipboard, switch view to plain text mode 

    I *think* this works all right... but traversing the heirarchy is another matter. From within subWidget:
    Qt Code:
    1. QMdiSubWindow * sw = dynamic_cast<QMdiSubWindow *>(parent()); // Returns a valid pointer
    2. QMdiArea * ws = dynamic_cast<QMdiArea *>(sw->parent()); // Returns NULL
    3. formMainWindow * mw = dynamic_cast<formMainWindow *>(sw->parent()); // Also returns NULL
    To copy to clipboard, switch view to plain text mode 
    So... what is the parent of a QMdiSubWindow? How can I traverse the heirarchy back to the formMainWindow?
    Life without passion is death in disguise

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: parent() heirarchy misunderstanding?

    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.

  3. The following user says thank you to marcel for this useful post:

    KShots (16th October 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: parent() heirarchy misunderstanding?

    Thanks, I was able to replace that with a simple
    Qt Code:
    1. static_cast<formMainWindow *>(parent()->parent()->parent()->parent())->subWindowDestroyed(this);
    To copy to clipboard, switch view to plain text mode 
    within the closeEvent(QCloseEvent *) of the subwindow (to clean up before the thing is actually destroyed).
    Life without passion is death in disguise

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.