Setting global variable in main() [Solved]
Hi all,
I have a Qt application and I need to set a variable (suppose a QString) in my main() that is visible in any part of my code without passing it to all class I created.
Es:
- ..
- ...
- main(...)
- {
- ...
- ...
- MyClass my_class;
- ...
- return a.exec();
- }
- MyClass::MyClass()
- {
- ...
- qDebug() << my_global_string;
- ...
- }
Is it possible using Qt?
7 replies
Than you have to use an extern statement in the other source file
- QString my_global_string;
- ..
- ...
- main(...)
- {
- ...
- ...
- MyClass my_class;
- ...
- return a.exec();
- }
- MyClass::MyClass()
- {
- ...
- qDebug() << my_global_string;
- ...
- }
I am trying to find a link to support you.
This elaborates on the issue: extern variable [en.wikipedia.org]
Personally, I dislike global variables. I find there are really no good use cases for them. What you can do instead, is use QApplication to hold your application wide data. You can either:
- Subclass QApplication, and give it proper access methods to your data, or
- Use Qt’ s dynamic property feature to just set a QVariant property on your QApplication object
However, it is just a personal dislike. The language supports it (and Qt does nothing that constrains your use of it), so go right ahead if you must.
Personally, I dislike global variables. I find there are really no good use cases for them. What you can do instead, is use QApplication to hold your application wide data. You can either:
- Subclass QApplication, and give it proper access methods to your data, or
- Use Qt’ s dynamic property feature to just set a QVariant property on your QApplication object
However, it is just a personal dislike. The language supports it (and Qt does nothing that constrains your use of it), so go right ahead if you must.
Thanks Andre,
I used a property on QApplication object this way:
- ..
- ...
- main(...)
- {
- ...
- a.setProperty("my_global_string", my_global_string);
- ...
- MyClass my_class;
- ...
- return a.exec();
- }
- MyClass::MyClass()
- {
- ...
- qDebug() << qApp->property("my_global_string");
- ...
- }
and it do the works!
Personally, I dislike global variables. I find there are really no good use cases for them. What you can do instead, is use QApplication to hold your application wide data. You can either:
- Subclass QApplication, and give it proper access methods to your data, or
- Use Qt’ s dynamic property feature to just set a QVariant property on your QApplication object
However, it is just a personal dislike. The language supports it (and Qt does nothing that constrains your use of it), so go right ahead if you must.
Andre, you are raising certainly a valid point with your response.
Global variables have always some “smell” of the old “FORTRAN common block”. Hard to controll because completely confusing when you are looking for the place changing some part of your data.
One must have no other alternative when using global variables. Otherwise they may be the start of disaster.
You must log in to post a reply. Not a member yet? Register here!





