Results 1 to 9 of 9

Thread: Access variables of other classes

  1. #1
    Join Date
    Jul 2010
    Location
    PL
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Access variables of other classes

    Hello, I have some problem - I cannot use a variable from other class.
    For example I have in class Setts QSpinBox SB1 and int i. I would like to check value of 'i' and set value of SB1 in method in class Pac.
    I tried Setts::set_ui->SB1->setValue(1); but I got error: object missing in reference to 'Setts::set_ui'

    setts.h
    Qt Code:
    1. #include <QSpinBox>
    2. #include "ui_setts.h>
    3.  
    4. namespace Ui {
    5. class Setts;
    6. }
    7.  
    8. class Setts : public QDialog {
    9. Q_OBJECT
    10. public:
    11. Setts(QWidget *parent = 0);
    12. ~Setts();
    13. Ui::Setts *set_ui;
    14.  
    15. protected:
    16. void changeEvent(QEvent *e);
    17.  
    18. private:
    19. QSpinBox *SB1;
    20. int i;
    21. }
    To copy to clipboard, switch view to plain text mode 

    setts.cpp
    Qt Code:
    1. #include "setts.h"
    2. #include "ui_setts.h"
    3.  
    4. Setts::Setts(QWidget *parent) :
    5. QDialog(parent),
    6. set_ui(new Ui::Setts)
    7.  
    8. {
    9. set_ui->setupUi(this);
    10. this->showFullScreen();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Thanks in advanced
    Przemek

  2. #2
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access variables of other classes

    What about moving variables from private to public? :P

  3. #3
    Join Date
    Jul 2010
    Location
    PL
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Access variables of other classes

    I tried, without results

  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: Access variables of other classes

    Use getter and setter methods. Also search the forum, there are hundreds post of that topic.

  5. #5
    Join Date
    Jul 2010
    Location
    PL
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Access variables of other classes

    Thanks Lykurg, but I have question. Is it alone way to use getter and setter methods? Is not possible to access variable without extra methods?

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access variables of other classes

    That is the reason why you declared your variables with "private" access specifier, so they can't be accessed/modified directly, only by their "public" getter/setter functions.

    LE: also notice that: Setts::set_ui->SB1->setValue(1); //most likely you tried that before constructing the actual object, set_ui is only a pointer... you construct your UI object in your class constructor... something like... set_ui = new UI_Class_Name(most likely 'this' as a parent); only after that you can do "stuff" to the UI pointer.

    When you use pointers you <need to> have two(2) objects, the pointer and the actual object that is "pointed to".
    Last edited by Zlatomir; 1st August 2010 at 13:07. Reason: lexical... 2

  7. The following user says thank you to Zlatomir for this useful post:

    przemek (1st August 2010)

  8. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access variables of other classes

    Sorry for double post... I edited (corrected) the last one a little bit, and a little advice

    Most likely, the setter functions you will want(need) to declare as slots, so that you can connect (other objects) signals to them.

  9. #8
    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: Access variables of other classes

    You can also use events or signals and slots.

  10. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Access variables of other classes

    I attached the sample project (as i promised )

    I used signals and slots and created some signals and slots for keeping the ui pointers private (you can do that, or you can inherit from both the ui_generated class and QDialog, QMainWindow or QWidget) i like (and use most) the private pointer member and code what signals and slots i need.VariablesUpdate.zip

Similar Threads

  1. Replies: 7
    Last Post: 11th April 2013, 10:55
  2. how to share variables
    By sksingh73 in forum Newbie
    Replies: 4
    Last Post: 2nd July 2010, 04:54
  3. environmental variables
    By mohanakrishnan in forum Newbie
    Replies: 4
    Last Post: 26th October 2009, 09:43
  4. How I get POST variables?
    By emental86 in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2009, 17:19
  5. QPainter and how to access the parent variables.
    By Teuniz in forum Qt Programming
    Replies: 6
    Last Post: 7th March 2007, 14:06

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.