August 17, 2011

Luca Luca
Ant Farmer
589 posts

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:

  1. ..
  2. ...
  3. main(...)
  4. {
  5. ...
  6. QString my_global_string("ciao");
  7. ...
  8. MyClass my_class;
  9. ...
  10.  
  11. return a.exec();
  12.  
  13. }

  1. MyClass::MyClass()
  2. {
  3. ...
  4. qDebug() << my_global_string;
  5. ...
  6. }

Is it possible using Qt?

7 replies

August 17, 2011

koahnig koahnig
Mad Scientist
2112 posts

Luca wrote:

  1. QString my_global_string;
  2. ..
  3. ...
  4. main(...)
  5. {
  6. ...
  7. my_global_string = QString  ("ciao");
  8. ...
  9. MyClass my_class;
  10. ...
  11.  
  12. return a.exec();
  13.  
  14. }

  1. MyClass::MyClass()
  2. {
  3. ...
  4. qDebug() << my_global_string;
  5. ...
  6. }

That should do the trick. Move the declaration out of your main routine.

August 17, 2011

Luca Luca
Ant Farmer
589 posts

I need to “see” the variable in other .cpp file too.

August 17, 2011

koahnig koahnig
Mad Scientist
2112 posts

Than you have to use an extern statement in the other source file

  1. QString my_global_string;
  2. ..
  3. ...
  4. main(...)
  5. {
  6. ...
  7. my_global_string = QString  ("ciao");
  8. ...
  9. MyClass my_class;
  10. ...
  11.  
  12. return a.exec();
  13.  
  14. }

  1. extern QString my_global_string;
  2.  
  3. MyClass::MyClass()
  4. {
  5. ...
  6. qDebug() << my_global_string;
  7. ...
  8. }

I am trying to find a link to support you.

August 17, 2011

koahnig koahnig
Mad Scientist
2112 posts

This elaborates on the issue: extern variable [en.wikipedia.org]

August 17, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 17, 2011

Luca Luca
Ant Farmer
589 posts

Andre wrote:
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:

  1. ..
  2. ...
  3. main(...)
  4. {
  5. ...
  6. QString my_global_string("ciao");
  7. QApplication a(argc, argv);
  8. a.setProperty("my_global_string", my_global_string);
  9. ...
  10. MyClass my_class;
  11. ...
  12.  
  13. return a.exec();
  14.  
  15. }

  1. MyClass::MyClass()
  2. {
  3. ...
  4. qDebug() << qApp->property("my_global_string");
  5. ...
  6. }

and it do the works!

August 17, 2011

koahnig koahnig
Mad Scientist
2112 posts

Andre wrote:
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.

 
  ‹‹ how to convert whole qt gui application from portrait to landscape mode      [Moved] How to display in an editor the result of encryption ››

You must log in to post a reply. Not a member yet? Register here!