Results 1 to 20 of 33

Thread: How to make all-files-global variable?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to make all-files-global variable?

    Hello, I have a project that has a lot of dialogs.
    In the constructor of the MainWindow I check a file in order to set the language. If, e.g. the language is Spanish the program turns to Spanish, if it is English it turns into English etc. The thing is that I have to do this in every single constructor of every dialog in order to check the language and do the appropriate changes in every dialog.
    Is it possible to use a all-files-global variable ? I don't want to perform the same check several times.
    (e.g.
    Qt Code:
    1. //this will be at the constructor of the MainWindow (mainwindow.cpp)
    2. if(the.file.says.the.program.to.be.english) english=1 //from here the other constructors of the other dialogs will check if english==1 and if so they'll do the changes
    To copy to clipboard, switch view to plain text mode 
    )
    Thx in advance
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    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: How to make all-files-global variable?

    If it is only for translation you should have a look at Qt internationalization functions. Otherwise have a look at [WIKI]Singleton Pattern[/WIKI].

    EDIT: Or use global functions/variables... But I am fine with the singleton pattern

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

    hakermania (9th December 2010)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make all-files-global variable?

    Ok, I simply want a fully global variale. I don't want to give singleton a chance right now, so how do I set a "fully" global value?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #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: How to make all-files-global variable?

    Well there are different approaches one is described here: http://www.learncpp.com/cpp-tutorial...bal-variables/. Or you can use global functions in a separate file with static variables and then do something like
    Qt Code:
    1. int languageCode()
    2. {
    3. static int code = -1;
    4. if (-1 == code)
    5. {
    6. // calculate code
    7. }
    8. return code;
    9. }
    To copy to clipboard, switch view to plain text mode 
    so the calculation will also only be done once.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make all-files-global variable?

    There is no concept of "fully global" variable. The variable is either global or not.

    http://www.cplusplus.com/doc/tutorial/variables/
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    hakermania (9th December 2010)

  8. #6
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make all-files-global variable?

    Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
    Anyway, I'll have a try at the singleton. Thanx both
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by hakermania View Post
    Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
    Hmm?? Did you follow the link I gave you?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by wysota View Post
    Hmm?? Did you follow the link I gave you?
    Ofcourse, but I know how to create a global variable! The problem is that I want to has access to this variable from all the .cpp files I use (as I said I have a lot of dialogs, every of which has its one .cpp file). That's why I named it "fully global" and not just global.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by hakermania View Post
    Ofcourse, but I know how to create a global variable! The problem is that I want to has access to this variable from all the .cpp files I use (as I said I have a lot of dialogs, every of which has its one .cpp file). That's why I named it "fully global" and not just global.
    What's the difference between "global" and "fully global"? Do you know the "extern" keyword from C?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    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: How to make all-files-global variable?

    Quote Originally Posted by wysota View Post
    Do you know the "extern" keyword from C?
    ...and that is perfectly described at the above mentioned site: http://www.learncpp.com/cpp-tutorial...bal-variables/.

  13. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by hakermania View Post
    The problem is that I want to has access to this variable from all the .cpp files I use (as I said I have a lot of dialogs, every of which has its one .cpp file). That's why I named it "fully global" and not just global.
    There's no such thing as a "fully global" variable. It's either global (Accessible by all files) or not. Maybe you are confusing global (accessible to all files in a project) and static (file-local) variables.

  14. #12
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make all-files-global variable?

    Do you know the "extern" keyword from C?
    Do you know the "extern" keyword from asm? Sorry, but I couldn't resist. Know your roots, people!

  15. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make all-files-global variable?

    No, as "asm" is meaningless without a processor architecture and manufacturer.

    For example, some packages used XDEF to define a symbol which could be exported, and XREF to import those same symbols.

  16. #14
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make all-files-global variable?

    MASM/Wintel/ALL (Intel and AMD share mnemonics).

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by Timoteo View Post
    Do you know the "extern" keyword from asm? Sorry, but I couldn't resist. Know your roots, people!
    My roots is Atari BASIC, there is no "extern" keyword there
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #16
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make all-files-global variable?

    Hah, my BASIC was the Commodore flavor. How old are you?

    Atari had a cartridge based assembler didn't it? Crazy stuff.

  19. #17
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by hakermania View Post
    Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
    Think about the future. Sure, you only want one variable now, but in a week, month or so you may two variables. Do you change your strategy (and the code already written for the first variable) or do something that is scalable? Thats what people are trying to teach you.

    If you are not interested in that, fine, wysota gave you a perfectly usable link for creating a simple global variable.

    Just note that simple global variables are fine for "bedroom coding", but are typically rejected for "workplace coding" and most multi-author and large-scale open-source projects.

  20. #18
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to make all-files-global variable?

    Singletons are vastly overrated and overused; in reality, they are nothing more than global variables in disguise, and they carry all of the shortcomings of globals along with them while hiding behind an OO veneer. And they bring their own problems to the table, as well.

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by SixDegrees View Post
    Singletons are vastly overrated and overused; in reality, they are nothing more than global variables in disguise, and they carry all of the shortcomings of globals along with them while hiding behind an OO veneer. And they bring their own problems to the table, as well.
    I would disagree. With a singleton you have much greater control over the object than with a regular global variable. A global variable is created before the main() function is executed and is destroyed after main() returns which can cause many problems (try declaring a global pixmap in Qt). With singleton this is not the case. Furthermore it lets you safely use C++ paradigms such as inheritance. The article you mention is also irrelevant here - it describes use of the singleton pattern against passing data as arguments, not against using global variables. All solutions have their pros and cons, you can't say "singletons are bad" just because they don't solve a particular problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  22. #20
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make all-files-global variable?

    Quote Originally Posted by hakermania View Post
    Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
    Anyway, I'll have a try at the singleton. Thanx both
    This being 2010, many decades of studying design techniques and measuring the success of the projects that use them...people have pretty much universally decided that global variables are to be avoided at nearly all cost.

    Actually, people knew this a long time ago. Most people discover it on their own if they don't learn from those ahead of them in the field.

    You might have a case for using one. I'd probably look for a different way but I'm not working on your project so...wtf do I know. At any rate, there's no reason to go through the incredibly expensive effort of altering a language for something like this. Declaring and defining global variables is already fairly easy, though it could be admitted that someone brand new could find it unintuitive.

    I followed the link you were given, didn't see what you need to know there. Here's how to do it:

    In a header file:
    Qt Code:
    1. extern type my_var;
    To copy to clipboard, switch view to plain text mode 

    This is a declaration.

    In ONE .cpp file:
    Qt Code:
    1. type my_var = a_value;
    To copy to clipboard, switch view to plain text mode 

    This is a definition AND an initialization. I suggest initializing your variable, but if you don't it will be "default initialized" (set to 0 in case of an int). You could also initialize through a function call, and in fact I use this a LOT for factory registration (a bigger topic). Just remember that anything you use to initialize your variable will happen before main is called and you have *absolutely no way of knowing in which order* except within specific cpp files.

    Once you've done the above two things you can use that variable anywhere that includes the .h you declared it in.

    Technically you don't have to have a header, but the reasons behind that would just confuse the hell out of you. Just do it this way...if you really must use a global variable.
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

Similar Threads

  1. Replies: 2
    Last Post: 21st October 2010, 07:03
  2. global variable in QT: getting ISSUES
    By Girija in forum Qt Programming
    Replies: 8
    Last Post: 19th September 2010, 16:15
  3. Setting a global variable
    By Windsoarer in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2010, 22:37
  4. how to declare a global variable right
    By Cruz in forum Newbie
    Replies: 13
    Last Post: 16th February 2010, 16:25
  5. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19

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.