Results 1 to 7 of 7

Thread: variable scope question

  1. #1
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default variable scope question

    I have declared a QSpinBox in the constructor of a dialog like this

    QSpinBox *recordNumberSpinBox = new QSpinBox;

    and also declared it as public: in the header file.

    But im getting core dumps when i try to do anythting with this QSpinBox in any other methods.

    for instance setting a maximum

    recordNumberSpinBox->setMaximum(5); in the method

    void Dialog:penDataFile(QFileInfo &fileInfo){
    ....
    }

    Am i confused in thinking that setting this to public means any method can access it and that its been set globaly?

  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: variable scope question

    So in effect you have two copies, one in your constructor and one in the class.

    Perhaps what you should do is delete the one in your constructor so the class scope takes over, then you can use it in other methods.

  3. The following user says thank you to squidge for this useful post:

    jeffmetal (17th June 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: variable scope question

    If you are still confused, after squidge's post, then the simple way say is modify your constructor like this

    Qt Code:
    1. //QSpinBox *recordNumberSpinBox = new QSpinBox;
    2. recordNumberSpinBox = new QSpinBox;
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Santosh Reddy for this useful post:

    jeffmetal (17th June 2011)

  6. #4
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: variable scope question

    Quote Originally Posted by Santosh Reddy View Post
    If you are still confused, after squidge's post, then the simple way say is modify your constructor like this

    Qt Code:
    1. //QSpinBox *recordNumberSpinBox = new QSpinBox;
    2. recordNumberSpinBox = new QSpinBox;
    To copy to clipboard, switch view to plain text mode 
    Whats the diffence between the two lines above and should i also leave the QSpinBox *recordNumberSpinBox; in the header ?

  7. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: variable scope question

    The difference is that first line is declaration of a pointer, visible only in current method, that is constructor. The recordNumberSpinBox member variable is not initialized, so you have a crash when trying to access it. If you want to use the spinbox in other methods, you should leave the declaration in the header, and initialize the member variable:
    Qt Code:
    1. this->recordNumberSpinBox = new QSpinBox();
    To copy to clipboard, switch view to plain text mode 
    Consider this example, it's just like your issue:
    Qt Code:
    1. class A{
    2. protected:
    3. int var; //member variable, every object of class A has its own var
    4. public:
    5. A(){
    6. this->var = 10; // initializes this object member variable with value 10
    7. int var = 1; // creates local variable named var, visible only in this constructor
    8. std::cout << "var = " << var << ", this->var = " << this->var << "\n";
    9. // prints: var = 1, this->var = 10
    10. }
    11. void method(){
    12. int x = var + 1; // x = 11, because this objects member variable is used
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

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

    jeffmetal (17th June 2011)

  9. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: variable scope question

    Leave it in the header file

  10. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: variable scope question

    The first line declares and initialises a variable in the local scope to point at a block of memory. This variable masks any other variable of the same name in a broader scope, e.g. a class member variable or global. The line variable becomes unavailable at the end of the scope enclosing it (although memory remains allocated). The value of the outer variable remains unchanged, i.e. it does not point at the allocated memory afterward. Compilers will sometimes issue a warning that a local variable masks another variable of the same name.

    The second line initialises a variable declared at a broader scope.

Similar Threads

  1. Variable not declared in this scope error
    By hojoff79 in forum Newbie
    Replies: 1
    Last Post: 30th December 2010, 01:29
  2. qmake debug/release scope question
    By redoctober0 in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 21:41
  3. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 21:48
  4. QT and variable scope
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 22:32
  5. Variable question
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 15th March 2007, 15:59

Tags for this Thread

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.