Results 1 to 15 of 15

Thread: How can I access public variable parent Form

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How can I access public variable parent Form

    Hi, I have a form and there is a pushbutton on the form. When I click the pushbutton , I open new Form by name sampleForm. How can I access public variable of parent class from sampleForm.

  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: How can I access public variable parent Form

    Many ways...
    1) Pass it to the child form, in the constructor.
    2) Make a setter method in the child form and set the variable after you instantiate the form
    3) Access it directly from the parent form, like this ((ParentForm*)parent())->yourPublicVar. But this is very ugly, since you would have to include the ParentForm here just for that.

    I recommend 1 or 2.

  3. #3
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I access public variable parent Form

    When I include the header file of the parent form, it throws me an error, that the current class is not declared in the parent form. What I mean is:

    I have the parent form

    #include "childForm.h"

    class parentForm
    {
    ...
    childForm ch ;
    } ;

    and the child form

    #include "parentForm.h"

    class childForm
    {
    ...
    } ;

    When I add #include "parentForm.h", then it says that it doesn't recognize the childForm type in the declaration of the parentForm. Obviously there is a confilct. Do you know some way to overcome it ?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I access public variable parent Form

    use forward declaration insted of including headers, i.e.
    Qt Code:
    1. ...
    2. class childForm;
    3.  
    4. class parentForm
    5. {
    6. ...
    7. childForm *ch ;
    8. } ;
    9. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I access public variable parent Form

    I have already tried this. The thing is, that I create a Form with QtDesigner, which is the parent class. Inside the form I put a class of my own, that inherits QWidget and this is the child form. If i do forward declaration, it says that I use functions that I have not implemented. I think this happens, because QtDesigner puts all the code inside the header file. I will try it again though, but I don't think it is sufficient. Any other ideas ?

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I access public variable parent Form

    could you prepare compilable example and show it?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I access public variable parent Form

    I attach a folder with 3 files, the parent header from QtDesigner and the child.
    Attached Files Attached Files

  8. #8
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I access public variable parent Form

    The parent class is Ui_Form and the child class is paintimagewidget. I changed the names of the files. The original for the parent was ui_manysift.h and for the child paintimagewidget

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I access public variable parent Form

    there are two ways to set up ui
    1.
    Qt Code:
    1. //h-file
    2. #include "ui_manysift.h"
    3.  
    4. class PaintImageWidget : public QWidget, private Ui::Form
    5. {
    6. Q_OBJECT
    7. ....
    8. };
    9.  
    10. //cpp-file
    11. PaintImageWidget::PaintImageWidget(QWidget *parent)
    12. : QWidget(parent)
    13. {
    14. setupUi(this);
    15. }
    To copy to clipboard, switch view to plain text mode 
    2.
    Qt Code:
    1. //h-file
    2. #include "ui_manysift.h"
    3.  
    4. class PaintImageWidget : public QWidget
    5. {
    6. Q_OBJECT
    7. ....
    8. private:
    9. Ui::Form m_ui;
    10. };
    11.  
    12. //cpp-file
    13. PaintImageWidget::PaintImageWidget(QWidget *parent)
    14. : QWidget(parent)
    15. {
    16. m_ui.setupUi(this);
    17. }
    To copy to clipboard, switch view to plain text mode 
    PS. this is not compilable example, I can't do qmake && nmake.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #10
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I access public variable parent Form

    I had a mistake in forward declaration. I found it, so now it works . Thanx for the help anyway ...

  11. #11
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I access public variable parent Form

    Quote Originally Posted by spirit View Post
    there are two ways to set up ui
    I always wondered, is there a preferred way to do it? What are the advantages or disadvantages of either approach?

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I access public variable parent Form

    if modify second approch you can get more perfomance on compilation stage, i.e. if ui-file has been modified then only needed cpp file will be rebuild. this approach is very usefull in big project with many ui-files.
    Qt Code:
    1. //h-file
    2. #include <QWidget>
    3.  
    4. namespace Ui {
    5. class MyWidget;
    6. };
    7.  
    8. class MyWidget: public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyWidget(QWidget *parent = 0);
    14. virtual ~MyWidget();
    15.  
    16. private:
    17. Ui::MyWidget *m_ui;
    18. };
    19.  
    20. //cpp-file
    21. #include "mywidget.h"
    22. #include "ui_mywidget.h"
    23.  
    24. MyWidget::MyWidget(QWidget *parent)
    25. : QWidget(parent)
    26. {
    27. m_ui = new Ui::MyWidget;
    28. m_ui->setupUi(this);
    29. }
    30.  
    31. MyWidget::~MyWidget()
    32. {
    33. delete m_ui;
    34. m_ui = 0;
    35. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #13
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I access public variable parent Form

    So, in general there is no real advantage in any of the two first approaches, except that it's probably easiser to modify the second one to the third that you posted?

    The third approach looks very interesting, I guess it can save quite some compile time while developing an application.

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04

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.