Results 1 to 6 of 6

Thread: how to use a global vars in QT?

  1. #1
    Join Date
    Jan 2011
    Posts
    40
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to use a global vars in QT?

    Someone said you could write a static class and call it.
    But I just wondered if I could use a global varieties to initialize my program.
    Here is the code:
    Qt Code:
    1. extern int var_name ;
    2. void MainWindow::function_name_1()
    3. {
    4. int var_name;
    5. var_name=1;
    6. };
    7. void MainWindow::function_name_2()
    8. {
    9. some_other_function(var_name);
    10. };
    To copy to clipboard, switch view to plain text mode 
    The compiler says may be used uninitialized in this function.
    So how to fix it?Anyone could give me some suggestions to use the global vars?

  2. #2
    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: how to use a global vars in QT?

    Obviously, var_name is not initialized in the global scope.
    What you did is create another var_name variable in function_name_1 and set it to 1. But that is a local variable that stops existing at the end of the scope (the } character).

    You can remove
    Qt Code:
    1. int var_name;
    To copy to clipboard, switch view to plain text mode 
    in the function function_name_1.

    But, that means that function_name_1 MUST be called as the absolute first function at all times. Not very practical.

    Note that you can initialize a variable directly like this:

    Qt Code:
    1. int var_name = 1;
    To copy to clipboard, switch view to plain text mode 

    While you can use global variables without any problem, it is not in the style of C++ (object oriented) and it can easily lead to errors (which object uses the variable at which point in time? Is the value still correct? ...)

  3. #3
    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: how to use a global vars in QT?

    Further, the code snippet:
    Qt Code:
    1. extern int var_name ;
    To copy to clipboard, switch view to plain text mode 
    does not create a place to store an int. The extern keyword is used to specify that the variable is declared in a different file and that the linker will need to resolve this name.

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to use a global vars in QT?

    Quote Originally Posted by wter27 View Post
    The compiler says may be used uninitialized in this function.
    you have two functions. "function_name_1" initializes "var_name". "function_name_2" uses "var_name". What happens if you call "function_name_2" before you call "function_name_1"? - BINGO, you'd use an uninitialized variable, as the compiler said...

  5. #5
    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: how to use a global vars in QT?

    Quote Originally Posted by FelixB View Post
    What happens if you call "function_name_2" before you call "function_name_1"? - BINGO, you'd use an uninitialized variable, as the compiler said...
    That doesn't even matter, even if function_name_1 is called before function_name_2, var_name is uninitialised.

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to use a global vars in QT?

    Quote Originally Posted by tbscope View Post
    That doesn't even matter, even if function_name_1 is called before function_name_2, var_name is uninitialised.
    oh, now I see. he redefines "var_name"...

Similar Threads

  1. How to specify my own global typedefs
    By Gert van Kruiningen in forum Qt Programming
    Replies: 7
    Last Post: 20th January 2011, 06:11
  2. qmake, subdirs & global vars
    By jpujolf in forum Qt Tools
    Replies: 0
    Last Post: 21st May 2009, 10:05
  3. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 05:19
  4. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2007, 00:42
  5. Where does VS save its inviornment vars?
    By high_flyer in forum Installation and Deployment
    Replies: 5
    Last Post: 4th February 2006, 11:10

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.