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 global variable in QT?
Forum Updated to NodeBB v4.3 + New Features

How to declare global variable in QT?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 3.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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @TomNow99 said in How to declare global variable in QT?:

    I don't have other x function or variable.

    You have: https://doc.qt.io/qt-5/qwidget.html#x-prop

    Use ::x - but it's c++ basics I would guess and has nothing to do with Qt :)

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    3
    • T Offline
      T Offline
      TomNow99
      wrote on last edited by
      #5

      @Christian-Ehrlicher It was.... Wow :D Thank you very much! Yes, x and y is position :D Thank you!

      1 Reply Last reply
      0
      • T TomNow99

        Hello,

        I know that my question is not stricte about QT, but about c++ ( or programming ) , but in this project I use QT.

        So I would like to create something like std::cout. I have to only add iostream library and in every place I can write std::cout<<something.

        I would like to start with something easier, so I would like to have global int.

        I created class:

        #ifndef DEB_H
        #define DEB_H
        
        extern int x;
        
        class deb
        {
        public:
            deb();
        };
        
        #endif // DEB_H
        
        #include "deb.h"
        
        int x=4;
        
        deb::deb()
        {
        
        }
        
        

        and add this class to mainWindow. In mainWindow I try do:

        qDebug()<<x;
        

        But I have error: reference to non-static member function must be called. Where I did wrong?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #6

        @TomNow99
        If in your deb.h file you show you have int x=4; in addition to extern int x;, then at least when you include that into multiple .cpp files you will end up with "multiple definitions of x variable" when you link. Definitions/initializations of "global" variables belong in one .cpp file, not in a .h file. EDIT Ah perhaps you are already doing this, I can't figure where what you show is situated.

        OK, I see what is @Christian-Ehrlicher is spotting. One obvious conclusion: if you are going to have "global" variables, don't name them anything like x!!

        1 Reply Last reply
        1
        • T Offline
          T Offline
          TomNow99
          wrote on last edited by
          #7

          @JonB When I delete this line "extern int x" from deb.h I have error in mainWindow "use of undeclared identifier xx".

          JonBJ 1 Reply Last reply
          0
          • T TomNow99

            @JonB When I delete this line "extern int x" from deb.h I have error in mainWindow "use of undeclared identifier xx".

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #8

            @TomNow99
            Of course, why would you delete the extern?? As I said, I was talking about the int x = 4 line, which I (at least originally) thought was in your deb.h. If that is in a .cpp and not a .h then you are fine....

            1 Reply Last reply
            1
            • T Offline
              T Offline
              TomNow99
              wrote on last edited by
              #9

              @JonB @Christian-Ehrlicher I have one more question.

              I need only one thing to have everything, what I would like.

              When I look at std::cout I can use it everywhere ( of course when I include iostream library ), but I can't create next std::ostream object ( because of protected constructor ). I would like the same in my project ( can't create other than that global object ).

              But when I change public to private constructor of my class, I will not create object in deb.cpp ( here I would like to create my object. Only here! ). I know that I can create some function singleton, but I would like that trick, which is in iostream and cout.

              So I would like have:

              #ifndef DEB_H
              #define DEB_H
              
              class deb
              {
              private:
                  deb();
              };
              
              extern deb xx;
              #endif // DEB_H
              

              and

              #include "deb.h"
              
              deb xx; // error - constructor is private
              
              deb::deb()
              {
              
              }
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #10

                You mix something up - std::cout is no std::ostream - it's a global object which takes a std::ostream via the << operator.
                std::ostream is a base class of the different output possibilities like iostream or ofstream.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • T Offline
                  T Offline
                  TomNow99
                  wrote on last edited by TomNow99
                  #11

                  @Christian-Ehrlicher Here:

                  https://en.cppreference.com/w/cpp/io/cout

                  I see ( on the top ):

                  extern std::ostream cout;
                  

                  EDIT:
                  So I tried create std::ostream cout2; and I can't

                  EDIT2:
                  So cout is object of class... ? Which one?

                  jsulmJ 1 Reply Last reply
                  0
                  • T TomNow99

                    @Christian-Ehrlicher Here:

                    https://en.cppreference.com/w/cpp/io/cout

                    I see ( on the top ):

                    extern std::ostream cout;
                    

                    EDIT:
                    So I tried create std::ostream cout2; and I can't

                    EDIT2:
                    So cout is object of class... ? Which one?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @TomNow99

                    extern std::ostream cout;
                    

                    declares a variable, so an instance/object.

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

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TomNow99
                      wrote on last edited by
                      #13

                      @jsulm Yes, but here I see that cout is object of std::sotream class.

                      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