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. Setting global variable in main() [Solved]
Forum Updated to NodeBB v4.3 + New Features

Setting global variable in main() [Solved]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 21.4k Views 1 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

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

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on last edited by
                #7

                [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!

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

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

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0

                  • Login

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