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. Global data
Forum Updated to NodeBB v4.3 + New Features

Global data

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.1k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    knetcozd
    wrote on last edited by
    #1

    Hi guys , i am new to QT and Desktop development as a whole (i am also new to C++) , Coming from a PHP web Dev background i am used to sessions and storing global information in sessions to share that data across pages , I am creating a small desktop app for a database back end and i have a main window with an admin button which when clicked shows a QDialog window and the user is asked to enter his login details , after processing the users logins i want to store a global variable i.e. role so that i can access it in the main window. I use this variable to hide/show admin related buttons - Whats the best way to do this ?

    Note : I have Googled this topic and have seen on other forums something about sub classing the QApplication object , I know how to subclass this but have no idea how to add the global variable or how to use the instance method.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      This is not really Qt related. It is a generic C++ question. Please learn the basics of the language you're using before starting to use the framework.

      Options for your case are:

      • using your QApplication instance to hold the data by setting custom (dynamic) properties or subclassing QApplication to hold additional data
      • Creating a singleton object to hold this data
      • Using global variables (not really recommended)
      • ...
      1 Reply Last reply
      0
      • K Offline
        K Offline
        knetcozd
        wrote on last edited by
        #3

        Yeah i hear what you saying about learning C++ and i have read through a few tutorials however i am between Java/Swing and C++/QT - I am just "test driving" the framework for now to see which best suits me , once i decide which to use i will defiantly invest more time in learning the respective language - Java / Swing is much easier to learn but C++/QT just has so many useful tools for GUI development thats hard to resist despite the learning curve.

        Getting back to the topic , as you mentioned using a Singleton object - i have attempted doing this but without much luck - I created a Singleton class and created a Singleton object in the main of my application but when i try to access the object inside the MainWindow class - its not visible. What am i doing wrong ?

        The singleton class
        @
        #ifndef SINGLETON_H
        #define SINGLETON_H

        class Singleton
        {
        public:

        void setRole(int i){role=i;};
        
        int getRole(){return role;};
        
        void setFloat(double i){floatAmt = i;}
        
        double getFloat(){return floatAmt;}
        

        private:

        int role;
        double floatAmt;
        

        };

        #endif // SINGLETON_H
        @

        Main method
        @
        extern Singleton SingletonObj;

        int main(int argc, char *argv[])
        {

        QApplication a(argc, argv);
        

        Singleton SingletonObj = Singleton();

        MainWindow w;
        w.show();
        return a.exec();
        

        }

        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          This is not how singletons work. It is a well-known design pattern, and just calling your class "Singleton" won't make it one. Google on "singleton pattern c++" to get tips on how to implement one.

          One basic thing you seem to forget in this case, however, is to include the header where you defined your "singleton" class into the main method.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Or use the tag search on the top right of this page and click on singleton. There was a discussion with sample code recently.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • K Offline
              K Offline
              knetcozd
              wrote on last edited by
              #6

              I did include the header files in the actual application - just left it out of the code from above extract so that it is more readable. Thanks guys for your help!!

              1 Reply Last reply
              0

              • Login

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