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(...)
    {
    ...
    QString my_global_string("ciao");
    ...
    MyClass my_class;
    ...

    return a.exec();

    }
    @

    @
    MyClass::MyClass()
    {
    ...
    qDebug() << my_global_string;
    ...
    }
    @

    Is it possible using Qt?


  • Moderators

    [quote author="Luca" date="1313573651"]
    @
    QString my_global_string;
    ..
    ...
    main(...)
    {
    ...
    my_global_string = QString ("ciao");
    ...
    MyClass my_class;
    ...

    return a.exec();

    }
    @

    @
    MyClass::MyClass()
    {
    ...
    qDebug() << my_global_string;
    ...
    }
    @

    [/quote]

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



  • I need to "see" the variable in other .cpp file too.


  • Moderators

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

    @
    QString my_global_string;
    ..
    ...
    main(...)
    {
    ...
    my_global_string = QString ("ciao");
    ...
    MyClass my_class;
    ...

    return a.exec();

    }
    @

    @
    extern QString my_global_string;

    MyClass::MyClass()
    {
    ...
    qDebug() << my_global_string;
    ...
    }
    @

    I am trying to find a link to support you.


  • Moderators

    This elaborates on the issue: "extern variable":http://en.wikipedia.org/wiki/External_variable



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



  • [quote author="Andre" date="1313579823"]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. [/quote]

    Thanks Andre,
    I used a property on QApplication object this way:

    @
    ..
    ...
    main(...)
    {
    ...
    QString my_global_string("ciao");
    QApplication a(argc, argv);
    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!


  • Moderators

    [quote author="Andre" date="1313579823"]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. [/quote]

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.