Results 1 to 12 of 12

Thread: Qt and global variables

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt and global variables

    Hi.
    I was wondering how can I use global variables in my Qt program? Where should I create the global Qt objects, in the main.cpp file (that is a very short file), or where?

    The other option is to create a global object and store a pointer to that object in everyother object that I then create (and that might be A LOT).

    Any suggestions?

  2. #2
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt and global variables

    I think that depends on the design of your program. I would never use global variables. They are not thread safe. If you need a single instantiating object do something like this (which is also not thread safe):
    Qt Code:
    1. class MyClass
    2. {
    3. MyClass() {};
    4. virtual ~MyClass() {};
    5.  
    6. public:
    7. static MyClass* instance()
    8. {
    9. if ( !m_pInstance )
    10. m_pInstance = new MyClass;
    11.  
    12. return m_pInstance;
    13. }
    14.  
    15. void doSomething()
    16. {
    17. // ...
    18. }
    19.  
    20. protected:
    21. static MyClass* m_pInstance;
    22. };
    23.  
    24. MyClass* MyClass::m_pInstance = NULL;
    To copy to clipboard, switch view to plain text mode 

    Now you could use "MyClass* pointer = MyClass::instance(); pointer->doSomething();".
    In that way you can also decide in which thread the instance will be created and no object exists before QApplication was instantiated. If you derive from QObject or not is your choice but don't forget to delete
    Last edited by gri; 23rd January 2007 at 20:20. Reason: updated contents

  3. #3
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Quote Originally Posted by Morea View Post
    Hi.
    I was wondering how can I use global variables in my Qt program?
    Why would you do that?

    Any suggestions?
    Yes, don't use them. Use the Singletton Pattern described by gri.


    Cheers.
    Kandalf
    There's no place like ~

  4. #4
    Join Date
    Nov 2006
    Posts
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Request you not to use global variables.
    Try to stick on to true OOP concepts.
    Qt is a wonderful framework which smoothly enables a
    developer to create applications in true object domain.

    I guess you are not trying to mix 'C' kind of coding using C++ compiler !

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    The singleton code looks nice. I'll try to use that. Thanks!

  6. #6
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Quote Originally Posted by Morea View Post
    The singleton code looks nice. I'll try to use that. Thanks!
    The Singleton implementation described is used widely but also contains a race-condition when it comes to use in a multithreaded program (see Alexandrescu's "Modern C++ Design" for further details).

    Qt Code:
    1. class MyClass
    2. {
    3. MyClass() {};
    4. virtual ~MyClass() {};
    5.  
    6. public:
    7. static MyClass* instance()
    8. {
    9. static QMutex mutex;
    10. if ( !m_pInstance )
    11. {
    12. mutex.lock();
    13. if (!m_pInstance)
    14. m_pInstance = new MyClass;
    15. mutex.unlock();
    16. }
    17.  
    18. return m_pInstance;
    19. }
    20.  
    21. void doSomething()
    22. {
    23. // ...
    24. }
    25.  
    26. protected:
    27. static MyClass* m_pInstance;
    28. };
    29.  
    30. MyClass* MyClass::m_pInstance = NULL;
    To copy to clipboard, switch view to plain text mode 
    Last edited by Methedrine; 24th January 2007 at 16:45. Reason: added code for thread-safe singleton

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

    sunil.thaha (2nd February 2007)

  8. #7
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Aye on the last post.

    Singleton's are a great concept but just be aware that you have to pay careful attention to making them thread safe.

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qt and global variables

    Methedrine, would you mind putting the example on our wiki? The singleton pattern has been suggested/discussed several times on the forums. One could just refer to the wiki article in the future..
    J-P Nurmi

  10. #9
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables


  11. The following user says thank you to Methedrine for this useful post:

    jpn (25th January 2007)

  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: Qt and global variables

    Sorry,

    maybe I am too stupid, but your code doesn't work for me. I take your code and just changed the name of the class:
    Qt Code:
    1. #ifndef CONFIGURATION_H
    2. #define CONFIGURATION_H
    3.  
    4. #include <QtCore>
    5. #include <QtSql>
    6.  
    7. class configuration
    8. {
    9. public:
    10. static configuration* instance()
    11. {
    12. static QMutex mutex;
    13. if ( !m_instance )
    14. {
    15. mutex.lock();
    16. if ( !m_instance )
    17. m_instance = new configuration;
    18. mutex.unlock();
    19. }
    20. return m_instance;
    21. }
    22.  
    23. private:
    24. configuration(){};
    25. configuration( const configuration& _instance ){};
    26. static configuration* m_instance;
    27.  
    28. // [...] some other functions
    29.  
    30. };
    31.  
    32. configuration* configuration::m_instance = NULL;
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

    When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
    .comp/sw_normal.o: In function `operator delete(void*, void*)':
    /usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
    .comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
    collect2: ld returned 1 exit status


    And by the way must it in your example in the wiki not be
    Qt Code:
    1. Singleton* Singleton::m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. Singleton* m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 



    Thanks,
    Lykurg

    P.s: if I comment the function (on bottom of the file) in qvortaro.cpp:194 out, then this error pointed to now last function on the file.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Quote Originally Posted by Lykurg View Post
    When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
    .comp/sw_normal.o: In function `operator delete(void*, void*)':
    /usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
    .comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
    collect2: ld returned 1 exit status
    You have placed the definition of static member variable in a header file, therefore every .cpp file that includes it has it's own copy of that variable. Better move that line to one of the .cpp files, say configuration.cpp.

  14. The following user says thank you to jacek for this useful post:

    Lykurg (1st February 2007)

  15. #12
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and global variables

    Quote Originally Posted by Lykurg View Post
    And by the way must it in your example in the wiki not be
    Qt Code:
    1. Singleton* Singleton::m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. Singleton* m_Instance = 0;
    To copy to clipboard, switch view to plain text mode 
    Yes, of course. Thank you, Lykurg for spotting it, and thank you, jacek, for correcting it before I spotted this thread

Similar Threads

  1. Replies: 1
    Last Post: 22nd January 2007, 10:41
  2. Declarate global parameters
    By Dark_Tower in forum Qt Programming
    Replies: 7
    Last Post: 11th December 2006, 19:01
  3. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 18:23
  4. Creating a global array in my code???
    By therealjag in forum General Programming
    Replies: 5
    Last Post: 13th March 2006, 12:13
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 20:56

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.