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. How to access my variables through qApp pointer?
Forum Updated to NodeBB v4.3 + New Features

How to access my variables through qApp pointer?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.1k Views 2 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.
  • C Offline
    C Offline
    CJha
    wrote on 21 Oct 2020, 11:16 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 21 Oct 2020, 11:18 last edited by
      #2

      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.

      (Z(:^

      C 1 Reply Last reply 21 Oct 2020, 11:29
      4
      • S sierdzio
        21 Oct 2020, 11:18

        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.

        C Offline
        C Offline
        CJha
        wrote on 21 Oct 2020, 11:29 last edited by
        #3

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

        S 1 Reply Last reply 21 Oct 2020, 11:34
        0
        • C CJha
          21 Oct 2020, 11:29

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

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 21 Oct 2020, 11:34 last edited by
          #4

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

          (Z(:^

          C 1 Reply Last reply 21 Oct 2020, 11:55
          5
          • S sierdzio
            21 Oct 2020, 11:34

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

            C Offline
            C Offline
            CJha
            wrote on 21 Oct 2020, 11:55 last edited by
            #5

            @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'.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 21 Oct 2020, 11:56 last edited by
              #6

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

              (Z(:^

              1 Reply Last reply
              6

              1/6

              21 Oct 2020, 11:16

              • Login

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