Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to access my variables through qApp pointer?



  • Hi, I am working on an application where I am controlling single hardware and I have multiple global variables related to it. I would like to store these variables in such a way that I am able to access it through a pointer such as qApp, for example, I can have a variable named isHActive which stores if the hardware is running or not and I can access it anywhere in my application using qApp->isHActive. Since qApp is available everywhere this will make it easier to access my main variables anywhere without having to #include any special files for my variables and thus avoiding the Circular Dependency problem (I know it's not a big problem, but sometimes it is very annoying to deal with it). What is the best way to achieve this?

    I tried subclassing QApplication but it didn't help. Following is the code that I tried:

    myapp.h

    #include <QtWidgets/QApplication>
    #define A (static_cast<MyApp *>(QApplication::instance()))
    
    class MyApp : public QApplication
    {
        Q_OBJECT
    
    public:
        MyApp(int &argc, char **argv);
        ~MyApp();
        bool isHActive{false};
    };
    

    myapp.cpp

    #include "myapp.h"
    
    MyApp::MyApp(int &argc, char **argv)
        :QApplication(argc, argv)
    {
        
    }
    

    In the above code I tried to redefine A as a pointer to MyApp class but it is not working like qApp, I also tried overriding the qApp pointer in a similar manner:

    #define A (static_cast<MyApp *>(qApp))
    

    All of it does not give any error but is also not working. The only way I can access A is if I #include "myapp.h" in each class where I want to use my variable. Is there any way I can attach my variables to the qApp pointer or attach it to something else that is as easily accessible as qApp?


  • Moderators

    Create a separate class, make it a singleton. Then it will be globally available (but you need to #include it, of course).

    Warning: singletons are introducing global state into an application and are easy to misuse.



  • @sierdzio Yes, I understand I can use a singleton class (and also the fact that it is easy to misuse it, but I am the only developer for this application so that's not a concern for me till now), that's how I have been doing it till now. But to be able to use the variable everywhere I have to #include it everywhere and pass a pointer of the original singleton object to the class either in the constructor or assign it afterward which in certain circumstances creates its own limitations.

    Not only that, the hardware I am controlling has its own limitations in terms of how I can access its functions, it is all in C and its functions have certain limitations such as some functions cannot be defined inside any other class, in which case accessing my variables through a pointer that I have to pass to each class becomes a problem. Accessing qApp is not a problem in such situations.


  • Moderators

    @CJha said in How to access my variables through qApp pointer?:

    But to be able to use the variable everywhere I have to #include it everywhere and pass a pointer of the original singleton object to the class either in the constructor or assign it afterward which in certain circumstances creates its own limitations.

    That's not how singletons work! You don't need to pass any pointers around. With a singleton, you get exactly the same result as with QApplication - all you need to do to get a pointer to your global object is something like:

    QApplication::instance();
    

    All that qApp macro does is to typedef it for you (both calls are synonyms):

    #define qApp (static_cast<QApplication *>(QCoreApplication::instance()))
    

    And yes, you do have to #include the header, but the same is true for qApp - you need to include QApplication to have access to it.



  • @sierdzio Thanks! this can solve my problem. I tried defining (my Singleton class is called Start and it inherits from QObject):

    #define qS (static_cast<Start*>(Start::instance()))
    

    But it is giving me error: 'Class "Start" has no member "instance".' Is there any way I can define instance? I tried to define it but I can only return this pointer from a non-static member function but then while accessing qS it gives an error saying 'a nonstatic member reference must be relative to a specific object'.


  • Moderators

    Here is a good tutorial on singletons: http://www.yolinux.com/TUTORIALS/C++Singleton.html


Log in to reply