Global data

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

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

  • 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

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


    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;;
    return a.exec();



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

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

  • 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!!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.