Results 1 to 9 of 9

Thread: Referencing Parent Widget from Child

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Referencing Parent Widget from Child

    Quote Originally Posted by taylor34
    I'd like to do that class cast, but I can't because my child widget class has no knowledge of the parent class (i.e. I'm not including the parent.h file or anything).
    When you are starting to put those little arrows one after the other the compiler generates indermediate objects / pointers to objects, so not including the interface of the generated objects the compiler won't know what to do and also you can only have access to the public interface:
    Qt Code:
    1. parent()->DoSomething();
    To copy to clipboard, switch view to plain text mode 
    is actually
    Qt Code:
    1. QObject * temp = parent();
    2. temp->DoSomething(); // boom
    To copy to clipboard, switch view to plain text mode 
    while you need
    Qt Code:
    1. #include "myform.h"
    2. //...
    3. MyForm *temp = (MyForm *)parent();
    4. temp->DoSomething()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows
    Thanks
    5

    Default Re: Referencing Parent Widget from Child

    while you need
    Qt Code:
    1. #include "myform.h"
    2. //...
    3. MyForm *temp = (MyForm *)parent();
    4. temp->DoSomething()
    To copy to clipboard, switch view to plain text mode 
    I tried that but I'm getting segmentation fault which means that I'm getting the wrong pointer from parent().

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: Referencing Parent Widget from Child

    Quote Originally Posted by Lebowski
    I tried that but I'm getting segmentation fault which means that I'm getting the wrong pointer from parent().
    did you checked the validity of the casted pointer ???
    basically, you should test for a NULL pointer each time you need to access such a pointer...

    Qt Code:
    1. if ( !temp )
    2. qDebug("crap!!!");
    3. else
    4. temp->doSomething()
    To copy to clipboard, switch view to plain text mode 

    That would do! If you get the "crap!!!" message, check the validity of your algorithm (I mean, be sure that you're not working with the bad pointer type) or try other ways of casting your pointer (RTTI may help).
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Referencing Parent Widget from Child

    You can use dynamic_cast or qobject_cast here to stand a chance when guessing the parent type.

Similar Threads

  1. let parent widget grow with child widget?
    By BeS in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 11:17
  2. Child Widget is hogging the MouseReleaseEvent
    By Cruz in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2009, 18:28
  3. How to close parent window from child window?
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2008, 11:40
  4. minimize child widget
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2006, 12:02
  5. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00

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
  •  
Qt is a trademark of The Qt Company.