Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] access data globally within application
QtWS25 Last Chance

[solved] access data globally within application

Scheduled Pinned Locked Moved General and Desktop
globalvariable
4 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sachi
    wrote on last edited by sachi
    #1

    I am developing qt application which users can log. what is the best way to keep user details access globally within a application. global variable or static variable or there is another better way?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's not the role of static variables and global variables are rather frown upon. You should rather consider using a singleton for that kind of things

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sachi
        wrote on last edited by
        #3

        can you explain more about singleton and its implementation. may be tutorial about how to use singleton on qt?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          slesa
          wrote on last edited by
          #4

          The singleton is part of the software design patterns.

          The implementation is easy: make the ctor private and implement one public static function which creates the instance:

          class Foo {
              private:
                static Foo* _instance;
                Test();
              public:
               static Foo* getInstance() {
                if (_instance==NULL)
                  _instance = new Foo();
                return _instance;
              }
          

          cpp:

          Foo::_instance = NULL;
          

          And, please, put not all 'global variables' into one place, but consider the SOLID principles ;)

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved