Results 1 to 8 of 8

Thread: how to get the pointer to parent widget/object?

  1. #1
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question how to get the pointer to parent widget/object?

    Hello,

    I would like to call a member function of my mainFrame class from my other Plot class. To do so, I need a pointer to mainFrame class within Plot class. Is there some call like : getParent() or something like this?

    In constructor of my Plot class, I already pass the pointer of mainFrame class, so how can I get it then?
    Qt Code:
    1. Plot::Plot(QWidget *parent):
    2. QwtPlot(parent)
    3. {
    To copy to clipboard, switch view to plain text mode 

    Thank you for help.

    Vitali

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get the pointer to parent widget/object?

    The parent is already given to your constructor, so just store it somewhere?

    Alternatively, you can use parentWidget()

  3. #3
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to get the pointer to parent widget/object?

    It does not work, if I use "parentWidget()->updateChromosome()". The error says:
    plot.cpp:453: error: 'class QWidget' has no member named 'updateChromosome'

    my mainFrame looks like this:

    mainframe.h:
    Qt Code:
    1. #include "plot.h"
    2. #include <qwt_plot_picker.h>
    3.  
    4. using namespace std;
    5.  
    6. class mainFrame : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. mainFrame(QWidget *parent = 0);
    12.  
    13. ...
    To copy to clipboard, switch view to plain text mode 

    mainframe.cpp:
    Qt Code:
    1. mainFrame::mainFrame(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5. ...
    6. d_plot = new Plot(this);
    7. layout_plot->addWidget(d_plot);
    To copy to clipboard, switch view to plain text mode 

    So how to get the pointer to the mainFrame class?

    My idea was to include mainframe.h into plot.h and declare there
    Qt Code:
    1. mainFrame *_mf;
    To copy to clipboard, switch view to plain text mode 
    and then store the passed pointer to it, but I have already includede plot.h into mainframe.h, so there will be conflict!

    Help me please.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to get the pointer to parent widget/object?

    You just have to cast the pointer to your class.

  5. #5
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to get the pointer to parent widget/object?

    In this case it is not possible, because I have already included the definition of class "Plot" into the header file of class "mainFrame", so that I cannot include the definition of class "mainFrame" into the header or cpp file of class "Plot"

    If I however do so (#inlcude "mainframe.h" in plot.cpp) and then:
    Qt Code:
    1. mainFrame *mf = dynamic_cast<mainFrame*>(parentWidget());
    2. mf->updateChromosome( selected_Points[i]->value(),index);
    To copy to clipboard, switch view to plain text mode 

    then I get this error:
    plot.h:11: error: redefinition of 'class Plot'
    plot.h:12: error: previous definition of 'class Plot'


    So I must somehow trick it around, but how?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to get the pointer to parent widget/object?

    Quote Originally Posted by rambo83 View Post
    then I get this error:
    plot.h:11: error: redefinition of 'class Plot'
    plot.h:12: error: previous definition of 'class Plot'


    So I must somehow trick it around, but how?
    Then is your header file wrong! use something like:
    Qt Code:
    1. #ifndef PLOT_H
    2. #define PLOT_H
    3.  
    4. class Plot : ...
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to get the pointer to parent widget/object?

    Quote Originally Posted by rambo83 View Post
    In this case it is not possible, because I have already included the definition of class "Plot" into the header file of class "mainFrame", so that I cannot include the definition of class "mainFrame" into the header or cpp file of class "Plot"
    You can use a forward declaration!

    Qt Code:
    1. class mainFrame;
    2.  
    3. class Plot
    4. {
    5. public:
    6. Plot(mainFrame *mf = 0);
    7. //...
    8. };
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lykurg for this useful post:

    rambo83 (26th November 2009)

  9. #8
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to get the pointer to parent widget/object?

    Yes, you are right, Lykurg!

    I have just found something about forward declaration in Google and used it in my code and it works now! Thank you very much.

    best regards,

    Vitali

Similar Threads

  1. Dragging a pointer
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2009, 04:51
  2. Replies: 6
    Last Post: 8th July 2009, 14:24
  3. get QMdiSubWindow Pointer of Current SubWindow
    By pospiech in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 05:40
  4. parent() heirarchy misunderstanding?
    By KShots in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2007, 19:18
  5. get Parent Pointer
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2006, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.