Results 1 to 7 of 7

Thread: variable scope question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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 

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

    jeffmetal (16th June 2011)

Similar Threads

  1. Variable not declared in this scope error
    By hojoff79 in forum Newbie
    Replies: 1
    Last Post: 30th December 2010, 00:29
  2. qmake debug/release scope question
    By redoctober0 in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 20:41
  3. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 20:48
  4. QT and variable scope
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 21:32
  5. Variable question
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 15th March 2007, 14: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
  •  
Qt is a trademark of The Qt Company.