Page 1 of 2 12 LastLast
Results 1 to 20 of 33

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

  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
    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.

  11. #9
    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.

  12. #10
    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.


  13. #11
    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.

  14. #12
    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.


  15. #13
    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/.

  16. #14
    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.

  17. #15
    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!

  18. #16
    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.

  19. #17
    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).

  20. #18
    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?

    That would be fine if Qt was only for Intel/AMD, but it's clearly not. Some people only use Qt on ARM platform, for example.

    If you changed your wording to 'Do you know the "extern" keyword from masm?' it would make perfect sense.

  21. #19
    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?

    For anybody who follows thisthread the solution is the following:
    make an new header file and include it into your project. the header file must inlude
    Qt Code:
    1. extern int IntName;
    To copy to clipboard, switch view to plain text mode 
    . Then put #include "headerfile.h" into the mainwindow.cpp file, and after all the inclusion type int IntName = 1(or any other value). Then you can have check for e.g. the language at the constructor of the mainwidow.cpp and include the header file into the preferences.cpp. then, in the preferences.cpp write after the inclusion int IntName; and then you will be able to check his value right now. That's all.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  22. #20
    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?

    My post was meant to be on the comical side of things. It didn't really have anything to do with Qt (not really sure why you are referring to it). I thought a few might have had experience with MASM and would have picked up on the reference. So much for that.

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.