Results 1 to 9 of 9

Thread: Referencing Parent Widget from Child

  1. #1

    Default Referencing Parent Widget from Child

    Hello--

    I've tried looking up through the database for this, but I couldn't find something exactly similar to what I'm doing. I'm using Qt 3.3 with designer (so I've got a bunch of ui.h files for my dialog boxes). In any case, I've got one parent dialog that opens a child dialog. When that child dialog does some actions, I'd like to change the color of a button on the parent dialog box to change with those actions. Is there any way to reference that parent widget from the child easily? I tried using (from inside the child widget):

    parent()->buttonOne->setPaletteForegroundColor(Color)

    but it doesn't like the parent() option. Perhaps I'm formatting it wrong? Thanks.

    Aaron

  2. #2
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Referencing Parent Widget from Child

    What is exactly the error message? Or does it crash? If it crashes, make sure you are creating the child dialog with this (the main or parent dialog) as the parent. Also make sure your buttonOne is public. However, this is poor design and brakes laws of encapsulation, abstraction, good OOP style etc... It would be better to maybe make a method setButtonBackgroundColor(QColor &newColor) or something like that, and then use it. I am not sure exactly of your situation and design so I am not sure what would work the best.
    Finally, what is Color? that should be a QColor variable, or one of predefined colors, like Qt::red.

    Bojan
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

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

    Default Re: Referencing Parent Widget from Child

    My wild guess is that you should cast parent() to the class of your parent widget, for example:

    Qt Code:
    1. ((MainWindow*)parent())->buttonOne->setPaletteForegroundColor(Color);
    To copy to clipboard, switch view to plain text mode 

  4. #4

    Default Re: Referencing Parent Widget from Child

    The error is is:

    'class QObject' has no member named 'buttonOne'

    btw, buttonOne is just the generic name I used for an example. My real code uses a name like taEditButton.

    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).

    Thanks again

    Aaron

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

    Default Re: Referencing Parent Widget from Child

    Then you can't access parents widgets, if you have no knowledge of them. You should somehow pass a pointer to the parent or move that functionality to the parent (which obviously has the knowledge of itself) for example using signals and slots.

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

    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 

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

    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().

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

    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.