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 namedisHActivewhich stores if the hardware is running or not and I can access it anywhere in my application usingqApp->isHActive. SinceqAppis available everywhere this will make it easier to access my main variables anywhere without having to#includeany 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
QApplicationbut 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
Aas a pointer toMyAppclass but it is not working likeqApp, I also tried overriding theqApppointer 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
Ais 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 theqApppointer or attach it to something else that is as easily accessible asqApp? -
Create a separate class, make it a singleton. Then it will be globally available (but you need to
#includeit, of course).Warning: singletons are introducing global state into an application and are easy to misuse.
-
Create a separate class, make it a singleton. Then it will be globally available (but you need to
#includeit, 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
#includeit 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
Cand 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. AccessingqAppis not a problem in such situations. -
@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
#includeit 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
Cand 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. AccessingqAppis not a problem in such situations.@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
qAppmacro does is to typedef it for you (both calls are synonyms):#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))And yes, you do have to
#includethe header, but the same is true forqApp- you need to includeQApplicationto have access to it. -
@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
qAppmacro does is to typedef it for you (both calls are synonyms):#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))And yes, you do have to
#includethe header, but the same is true forqApp- you need to includeQApplicationto have access to it.@sierdzio Thanks! this can solve my problem. I tried defining (my Singleton class is called
Startand it inherits fromQObject):#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
thispointer from a non-static member function but then while accessingqSit gives an error saying 'a nonstatic member reference must be relative to a specific object'. -
Here is a good tutorial on singletons: http://www.yolinux.com/TUTORIALS/C++Singleton.html