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. How to declare a global changeable variable?
QtWS25 Last Chance

How to declare a global changeable variable?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 15.5k Views
  • 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.
  • G Offline
    G Offline
    GCDX
    wrote on 19 Jun 2018, 08:24 last edited by
    #1

    Hi coders,
    I realised that I should not have use a DEFINES += variable in my QT.pro file, hence i was thinking of a way to change it to a global changeable variable in the app. But, after much googling and searching i could not find a solution, could someone point me in the right direction and show me documentation too?

    J 1 Reply Last reply 19 Jun 2018, 08:44
    0
    • G GCDX
      19 Jun 2018, 08:24

      Hi coders,
      I realised that I should not have use a DEFINES += variable in my QT.pro file, hence i was thinking of a way to change it to a global changeable variable in the app. But, after much googling and searching i could not find a solution, could someone point me in the right direction and show me documentation too?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 Jun 2018, 08:44 last edited by
      #2

      @GCDX Global variables are a sign of bad design.
      But if you really need it do:

      // In some header file
      extern int globalVar;
      
      // in some cpp file
      int globalVar = 0;
      

      Then include that header file where you need access to the global variable.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • A Offline
        A Offline
        Aditya1309
        wrote on 19 Jun 2018, 09:02 last edited by
        #3

        @GCDX said in How to declare a global changeable variable?:

        could someone point me in the right direction and show me documentation too

        Hi you can use extern variable declare in some .h header file and use .cpp file to define it. Since the scope is golbal it can be accessed from outside also.
        Take a look at this

        file3.h
        extern int global_variable; /* Declaration of the variable */

        file1.c
        #include "file3.h" /* Declaration made available here /
        #include "prog1.h" /
        Function declarations */

        /* Variable defined here /
        int global_variable = 37; /
        Definition checked against declaration */

        int increment(void) { return global_variable++; }

        file2.c

        #include "file3.h"
        #include "prog1.h"
        #include <stdio.h>

        void use_it(void)
        {
        printf("Global variable: %d\n", global_variable++);
        }

        https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 20 Jun 2018, 20:14 last edited by
          #4

          Hi
          Word of Warning.
          While one global variable won't ruin your life, using this method
          for sharing data between classes will soon be a mess.
          But i read it as you want one global variable to control something so
          you should be all right :)

          1 Reply Last reply
          1
          • G Offline
            G Offline
            GCDX
            wrote on 21 Jun 2018, 06:11 last edited by
            #5

            @mrjj @Aditya1309 Thanks for all the help

            M J 2 Replies Last reply 21 Jun 2018, 06:17
            0
            • G GCDX
              21 Jun 2018, 06:11

              @mrjj @Aditya1309 Thanks for all the help

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 21 Jun 2018, 06:17 last edited by
              #6

              @GCDX
              Can i ask what the global will control ?
              I understand you first use a #define from .pro file but need app to alter value.
              so i wonder what its role is :)
              Just to know.

              G 1 Reply Last reply 21 Jun 2018, 06:47
              0
              • G GCDX
                21 Jun 2018, 06:11

                @mrjj @Aditya1309 Thanks for all the help

                J Offline
                J Offline
                JonB
                wrote on 21 Jun 2018, 06:41 last edited by
                #7

                @GCDX
                #defines and "variables" have nothing to do with each other.

                Unless Qt has a good reason for advising otherwise(?), I see no reason why a .pro file should not have a DEFINES += ... for stuff you have a good reason to want to define.

                As @mrjj asks, perhaps it would be a good idea to explain what you are actually trying to achieve?

                G 1 Reply Last reply 21 Jun 2018, 06:48
                2
                • M mrjj
                  21 Jun 2018, 06:17

                  @GCDX
                  Can i ask what the global will control ?
                  I understand you first use a #define from .pro file but need app to alter value.
                  so i wonder what its role is :)
                  Just to know.

                  G Offline
                  G Offline
                  GCDX
                  wrote on 21 Jun 2018, 06:47 last edited by
                  #8

                  @mrjj I want to extract the API key of a user and save it as a global variable to access the other features of the website! I've already done it and it works!

                  1 Reply Last reply
                  0
                  • J JonB
                    21 Jun 2018, 06:41

                    @GCDX
                    #defines and "variables" have nothing to do with each other.

                    Unless Qt has a good reason for advising otherwise(?), I see no reason why a .pro file should not have a DEFINES += ... for stuff you have a good reason to want to define.

                    As @mrjj asks, perhaps it would be a good idea to explain what you are actually trying to achieve?

                    G Offline
                    G Offline
                    GCDX
                    wrote on 21 Jun 2018, 06:48 last edited by
                    #9

                    @JonB OKay i see, its because i was editing a QT code that was written by someone else. So he used a define and i assume define was a static global variable that is used but could not really find out how to write a dynamic global variable

                    J 1 Reply Last reply 21 Jun 2018, 06:50
                    0
                    • G GCDX
                      21 Jun 2018, 06:48

                      @JonB OKay i see, its because i was editing a QT code that was written by someone else. So he used a define and i assume define was a static global variable that is used but could not really find out how to write a dynamic global variable

                      J Offline
                      J Offline
                      JonB
                      wrote on 21 Jun 2018, 06:50 last edited by
                      #10

                      @GCDX
                      What did/does his DEFINE actually read?

                      G 1 Reply Last reply 21 Jun 2018, 07:02
                      0
                      • J JonB
                        21 Jun 2018, 06:50

                        @GCDX
                        What did/does his DEFINE actually read?

                        G Offline
                        G Offline
                        GCDX
                        wrote on 21 Jun 2018, 07:02 last edited by
                        #11

                        @JonB It needs an API key. However, the problem is that if too many use the API key it will be a threat to the server and the API key will get banned

                        J 1 Reply Last reply 21 Jun 2018, 07:05
                        0
                        • G GCDX
                          21 Jun 2018, 07:02

                          @JonB It needs an API key. However, the problem is that if too many use the API key it will be a threat to the server and the API key will get banned

                          J Offline
                          J Offline
                          JonB
                          wrote on 21 Jun 2018, 07:05 last edited by
                          #12

                          @GCDX
                          I can only imagine this means: at present there is a DEFINE for a constant number/string for some "API key", and you want to change that to be a variable which can be assigned at runtime (somehow) so that it can vary.

                          G 1 Reply Last reply 21 Jun 2018, 07:06
                          0
                          • J JonB
                            21 Jun 2018, 07:05

                            @GCDX
                            I can only imagine this means: at present there is a DEFINE for a constant number/string for some "API key", and you want to change that to be a variable which can be assigned at runtime (somehow) so that it can vary.

                            G Offline
                            G Offline
                            GCDX
                            wrote on 21 Jun 2018, 07:06 last edited by
                            #13

                            @JonB Yes, i want it to change such that every user who downloads the app can generate their own API key so a single API key won't be the one polling the system

                            J 1 Reply Last reply 21 Jun 2018, 07:07
                            0
                            • G GCDX
                              21 Jun 2018, 07:06

                              @JonB Yes, i want it to change such that every user who downloads the app can generate their own API key so a single API key won't be the one polling the system

                              J Offline
                              J Offline
                              JonB
                              wrote on 21 Jun 2018, 07:07 last edited by JonB
                              #14

                              @GCDX
                              Then you do indeed need to change over from the DEFINE to a variable! (The existing DEFINE is not a "static global variable", it's a [pre-processor] constant, which is quite different.)

                              G 1 Reply Last reply 21 Jun 2018, 07:08
                              1
                              • J JonB
                                21 Jun 2018, 07:07

                                @GCDX
                                Then you do indeed need to change over from the DEFINE to a variable! (The existing DEFINE is not a "static global variable", it's a [pre-processor] constant, which is quite different.)

                                G Offline
                                G Offline
                                GCDX
                                wrote on 21 Jun 2018, 07:08 last edited by
                                #15

                                @JonB Yes its global variable using extern in a globalvariable.h file.

                                M 1 Reply Last reply 21 Jun 2018, 07:12
                                0
                                • G GCDX
                                  21 Jun 2018, 07:08

                                  @JonB Yes its global variable using extern in a globalvariable.h file.

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 21 Jun 2018, 07:12 last edited by
                                  #16

                                  @GCDX
                                  And you also need to save it to a file or will user generate new each time?

                                  G 1 Reply Last reply 21 Jun 2018, 07:15
                                  0
                                  • M mrjj
                                    21 Jun 2018, 07:12

                                    @GCDX
                                    And you also need to save it to a file or will user generate new each time?

                                    G Offline
                                    G Offline
                                    GCDX
                                    wrote on 21 Jun 2018, 07:15 last edited by
                                    #17

                                    @mrjj do i need to use QFileDialog for this??

                                    J M 3 Replies Last reply 21 Jun 2018, 07:20
                                    0
                                    • G GCDX
                                      21 Jun 2018, 07:15

                                      @mrjj do i need to use QFileDialog for this??

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 21 Jun 2018, 07:20 last edited by JonB
                                      #18

                                      @GCDX
                                      Read up on http://doc.qt.io/qt-5/qsettings.html#details for an explanation (including sub-topic Platform-Specific Notes). In the usual case, you do not ask the user where to save the settings (your QFileDialog), you allow Qt to save it in the "default" place for your OS.

                                      QSettings automatically saves your changes for you.

                                      [EDIT: I'm not sure why I started talking about QSettings here, I thought you/@mrjj had mentioned it, but perhaps not --- I may have been getting mixed up with another question I was answering! Nonetheless, it's a possible way of saving something to file if that's what you want...]

                                      1 Reply Last reply
                                      0
                                      • G GCDX
                                        21 Jun 2018, 07:15

                                        @mrjj do i need to use QFileDialog for this??

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 21 Jun 2018, 07:25 last edited by
                                        #19

                                        @GCDX
                                        Only if user should select file name himself.
                                        else you will just save it using QFile to a writable location , you can find lookup using
                                        http://doc.qt.io/qt-5/qstandardpaths.html

                                        1 Reply Last reply
                                        1
                                        • G GCDX
                                          21 Jun 2018, 07:15

                                          @mrjj do i need to use QFileDialog for this??

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 21 Jun 2018, 07:28 last edited by
                                          #20

                                          @GCDX
                                          If you ask the user for a file name/path to save to, you are going to have to ask the user for the same path in order to read it back at a later date. This does not sound likely! More like @mrjj's suggestion of saving to a standard file name in a standard directory so that you can re-read it subsequently automatically.

                                          1 Reply Last reply
                                          1

                                          3/22

                                          19 Jun 2018, 09:02

                                          19 unread
                                          • Login

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