Results 1 to 10 of 10

Thread: How get a pointer to the parent widget

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How get a pointer to the parent widget

    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
    Last edited by franco.amato; 7th September 2010 at 19:37.
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How get a pointer to the parent widget

    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?

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get a pointer to the parent widget

    Quote Originally Posted by tbscope View Post
    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.
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How get a pointer to the parent widget

    Quote Originally Posted by franco.amato View Post
    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.

    Qt Code:
    1. MainWindow -> acts to widget1 signal in a slot
    2. |
    3. +---- Central widget
    4. |
    5. +---- Widget 1 -> sends a signal to the main window
    6. |
    7. +---- Widget 2
    8.  
    9.  
    10. connect(widget1, SIGNAL(widget1ActionEnabled(bool)), mainwindow, SLOT(slotWidget1ActionEnabled(bool)));
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get a pointer to the parent widget

    Quote Originally Posted by tbscope View Post
    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.

    Qt Code:
    1. MainWindow -> acts to widget1 signal in a slot
    2. |
    3. +---- Central widget
    4. |
    5. +---- Widget 1 -> sends a signal to the main window
    6. |
    7. +---- Widget 2
    8.  
    9.  
    10. connect(widget1, SIGNAL(widget1ActionEnabled(bool)), mainwindow, SLOT(slotWidget1ActionEnabled(bool)));
    To copy to clipboard, switch view to plain text mode 

    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?
    Franco Amato

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How get a pointer to the parent widget

    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:

    Qt Code:
    1. myMainWindow::myMainWindow(...) :
    2. {
    3. connect(ui->widget1, SIGNAL(...), this, SLOT(...));
    4. }
    5.  
    6. void myMainWindow::slotEnable(bool on)
    7. {
    8. if(on)
    9. ...
    10. else
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How get a pointer to the parent widget

    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
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How get a pointer to the parent widget

    Quote Originally Posted by wysota View Post
    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
    Franco Amato

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

    Default Re: How get a pointer to the parent widget

    Quote Originally Posted by franco.amato View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    May 2007
    Posts
    106
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How get a pointer to the parent widget

    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

Similar Threads

  1. Replies: 7
    Last Post: 14th January 2010, 08:47
  2. Replies: 7
    Last Post: 26th November 2009, 14:29
  3. Replies: 4
    Last Post: 3rd October 2009, 08:19
  4. using the parent-pointer
    By doktorn in forum Newbie
    Replies: 5
    Last Post: 31st August 2007, 18:59
  5. get Parent Pointer
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2006, 13:09

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.