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

Object as global variable

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 1.1k Views 4 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.
  • MortyMarsM Offline
    MortyMarsM Offline
    MortyMars
    wrote on last edited by
    #1

    Hi everyone,

    I'm a beginner on Qt, ... and I have a beginner's question.

    I'm trying to develop an application made up of two main windows (I know it's not right, but I want windows that are independent of each other because they are equally important for the application). Each of the two windows lets me enter information that I save in a text file, and I want to be able to switch from one window to the other and back to the first, without losing what I've just entered in either window.
    These windows are initialised in main.cpp .
    Once displayed, I switch from one to the other using a combined show() and hide() button, but the problem is that I generate a new blank window each time and my input is lost.

    So I thought of tracking each of the window objects from their first generation by making them global objects declared in a header file so that I could call them every time.
    'main.cpp' code :

    main.cpp.jpg

    'varglobales.h' code :
    varglobales.h.jpg

    But when I compile I get an error ' ' which stops the compilation...
    Compilation error.jpg

    What have I done wrong?

    Thanks for your feedback

    Christian EhrlicherC C 2 Replies Last reply
    0
    • MortyMarsM MortyMars

      Hi everyone,

      I'm a beginner on Qt, ... and I have a beginner's question.

      I'm trying to develop an application made up of two main windows (I know it's not right, but I want windows that are independent of each other because they are equally important for the application). Each of the two windows lets me enter information that I save in a text file, and I want to be able to switch from one window to the other and back to the first, without losing what I've just entered in either window.
      These windows are initialised in main.cpp .
      Once displayed, I switch from one to the other using a combined show() and hide() button, but the problem is that I generate a new blank window each time and my input is lost.

      So I thought of tracking each of the window objects from their first generation by making them global objects declared in a header file so that I could call them every time.
      'main.cpp' code :

      main.cpp.jpg

      'varglobales.h' code :
      varglobales.h.jpg

      But when I compile I get an error ' ' which stops the compilation...
      Compilation error.jpg

      What have I done wrong?

      Thanks for your feedback

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MortyMars said in Object as global variable:

      What have I done wrong?

      You declared a pointer but did not define it anywhere. You basically only told the compiler that there is a pointer to SidStarWindow but nothing more. You have to define it in one of your cpp files.
      And btw: global objects are not really good design and should be avoided.

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

      MortyMarsM 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @MortyMars said in Object as global variable:

        What have I done wrong?

        You declared a pointer but did not define it anywhere. You basically only told the compiler that there is a pointer to SidStarWindow but nothing more. You have to define it in one of your cpp files.
        And btw: global objects are not really good design and should be avoided.

        MortyMarsM Offline
        MortyMarsM Offline
        MortyMars
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in Object as global variable:

        You declared a pointer but did not define it anywhere. You basically only told the compiler that there is a pointer to SidStarWindow but nothing more. You have to define it in one of your cpp files.
        And btw: global objects are not really good design and should be avoided.

        Hi Christian,

        Thanks for your quick and efficient feedback: it was indeed a beginner's question (in C++ more than in Qt...).

        With this simple initialisation, it works the way I want it to!

        I understand that global objects are not desirable, but what would be the elegant alternative in my case?

        Thanks again!

        SGaistS 1 Reply Last reply
        0
        • MortyMarsM MortyMars has marked this topic as solved on
        • MortyMarsM MortyMars

          @Christian-Ehrlicher said in Object as global variable:

          You declared a pointer but did not define it anywhere. You basically only told the compiler that there is a pointer to SidStarWindow but nothing more. You have to define it in one of your cpp files.
          And btw: global objects are not really good design and should be avoided.

          Hi Christian,

          Thanks for your quick and efficient feedback: it was indeed a beginner's question (in C++ more than in Qt...).

          With this simple initialisation, it works the way I want it to!

          I understand that global objects are not desirable, but what would be the elegant alternative in my case?

          Thanks again!

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MortyMars hi and welcome to devnet,

          The usual answer would be: proper architecture.

          If you use global variable for ease of access then you are doing something very wrong.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          MortyMarsM 2 Replies Last reply
          1
          • MortyMarsM MortyMars

            Hi everyone,

            I'm a beginner on Qt, ... and I have a beginner's question.

            I'm trying to develop an application made up of two main windows (I know it's not right, but I want windows that are independent of each other because they are equally important for the application). Each of the two windows lets me enter information that I save in a text file, and I want to be able to switch from one window to the other and back to the first, without losing what I've just entered in either window.
            These windows are initialised in main.cpp .
            Once displayed, I switch from one to the other using a combined show() and hide() button, but the problem is that I generate a new blank window each time and my input is lost.

            So I thought of tracking each of the window objects from their first generation by making them global objects declared in a header file so that I could call them every time.
            'main.cpp' code :

            main.cpp.jpg

            'varglobales.h' code :
            varglobales.h.jpg

            But when I compile I get an error ' ' which stops the compilation...
            Compilation error.jpg

            What have I done wrong?

            Thanks for your feedback

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @MortyMars SIDs, STARs, and approaches ... welcome to my world :)

            MortyMarsM 1 Reply Last reply
            0
            • SGaistS SGaist

              @MortyMars hi and welcome to devnet,

              The usual answer would be: proper architecture.

              If you use global variable for ease of access then you are doing something very wrong.

              MortyMarsM Offline
              MortyMarsM Offline
              MortyMars
              wrote on last edited by
              #6

              @SGaist said in Object as global variable:

              @MortyMars hi and welcome to devnet,

              The usual answer would be: proper architecture.

              If you use global variable for ease of access then you are doing something very wrong

              Thank you for your welcome ;-)

              1 Reply Last reply
              0
              • C ChrisW67

                @MortyMars SIDs, STARs, and approaches ... welcome to my world :)

                MortyMarsM Offline
                MortyMarsM Offline
                MortyMars
                wrote on last edited by
                #7

                @ChrisW67 said in Object as global variable:

                @MortyMars SIDs, STARs, and approaches ... welcome to my world :)

                I'm trying to develop a tool to help edit approach files for X-Plane 12, because it's not impossible to do by hand, but it's pretty tedious...

                C 1 Reply Last reply
                0
                • MortyMarsM MortyMars

                  @ChrisW67 said in Object as global variable:

                  @MortyMars SIDs, STARs, and approaches ... welcome to my world :)

                  I'm trying to develop a tool to help edit approach files for X-Plane 12, because it's not impossible to do by hand, but it's pretty tedious...

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  @MortyMars I can imagine. They're a pain to design and describe in the first place.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @MortyMars hi and welcome to devnet,

                    The usual answer would be: proper architecture.

                    If you use global variable for ease of access then you are doing something very wrong.

                    MortyMarsM Offline
                    MortyMarsM Offline
                    MortyMars
                    wrote on last edited by
                    #9

                    @SGaist said in Object as global variable:

                    @MortyMars hi and welcome to devnet,

                    The usual answer would be: proper architecture.

                    If you use global variable for ease of access then you are doing something very wrong.

                    Hi SGaist,

                    After a few fruitless searches on the net, I have to admit that I'd be interested in a bit more detail about an alternative solution to global variables and the right architecture to put in place as you mention....

                    Thank you for any leads you can give me ;-)

                    SGaistS 1 Reply Last reply
                    1
                    • MortyMarsM MortyMars

                      @SGaist said in Object as global variable:

                      @MortyMars hi and welcome to devnet,

                      The usual answer would be: proper architecture.

                      If you use global variable for ease of access then you are doing something very wrong.

                      Hi SGaist,

                      After a few fruitless searches on the net, I have to admit that I'd be interested in a bit more detail about an alternative solution to global variables and the right architecture to put in place as you mention....

                      Thank you for any leads you can give me ;-)

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Well, the first thing to do is to identify why you think you need global variables.

                      What is your application architecture ?
                      What are you using them for ?

                      From there we can iterate to improve your architecture.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      MortyMarsM 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Well, the first thing to do is to identify why you think you need global variables.

                        What is your application architecture ?
                        What are you using them for ?

                        From there we can iterate to improve your architecture.

                        MortyMarsM Offline
                        MortyMarsM Offline
                        MortyMars
                        wrote on last edited by
                        #11

                        @SGaist said in Object as global variable:

                        Well, the first thing to do is to identify why you think you need global variables.

                        What is your application architecture ?
                        What are you using them for ?

                        From there we can iterate to improve your architecture

                        Thanks for your feedback.

                        My application is based around two windows that can be called up from each other. Each of these windows needs to be unique and remain for the entire life of the application, as data is entered in them and needs to be preserved.

                        I use the show() and hide() methods to hide and recall them, but the show() method (probably badly used) creates new objects of the window class each time I call it up and doesn't allow me to recall the two original windows.

                        Global variables seemed to me to be the solution for creating a lasting link and pointing to these two famous windows for sure.

                        If there's an alternative way of creating this durable link, then my problem is solved...

                        SGaistS 1 Reply Last reply
                        0
                        • MortyMarsM MortyMars

                          @SGaist said in Object as global variable:

                          Well, the first thing to do is to identify why you think you need global variables.

                          What is your application architecture ?
                          What are you using them for ?

                          From there we can iterate to improve your architecture

                          Thanks for your feedback.

                          My application is based around two windows that can be called up from each other. Each of these windows needs to be unique and remain for the entire life of the application, as data is entered in them and needs to be preserved.

                          I use the show() and hide() methods to hide and recall them, but the show() method (probably badly used) creates new objects of the window class each time I call it up and doesn't allow me to recall the two original windows.

                          Global variables seemed to me to be the solution for creating a lasting link and pointing to these two famous windows for sure.

                          If there's an alternative way of creating this durable link, then my problem is solved...

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @MortyMars ok so first, take a look at QStackedWidget which allows you to easily switch between multiple widgets.

                          I don't know about the data you need to share but it makes me think of the model view architecture. Your widgets are the views and you share a model between the two. Give your widgets as setModel method, store the model pointer in each of and voila, your global variable need has been eliminated.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          JonBJ 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            @MortyMars ok so first, take a look at QStackedWidget which allows you to easily switch between multiple widgets.

                            I don't know about the data you need to share but it makes me think of the model view architecture. Your widgets are the views and you share a model between the two. Give your widgets as setModel method, store the model pointer in each of and voila, your global variable need has been eliminated.

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

                            @SGaist said in Object as global variable:

                            ok so first, take a look at QStackedWidget which allows you to easily switch between multiple widgets.

                            I did not suggest this because: initially the OP talked about show() & hide(), so only one window/widget at a time. For which QStackedWidget would be perfect. But in his initial code notice he show()s them both. He then says

                            and I want to be able to switch from one window to the other and back to the first

                            It took it to mean he wants to see both at the same time. But we can't tell.

                            @MortyMars Simple question: do you want both windows visible at same time --- QStackedWidget no good --- or do you only want one at a time --- in which case it's fine/recommended, but then don't start out show()ing both of them. So which is it?

                            MortyMarsM 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @SGaist said in Object as global variable:

                              ok so first, take a look at QStackedWidget which allows you to easily switch between multiple widgets.

                              I did not suggest this because: initially the OP talked about show() & hide(), so only one window/widget at a time. For which QStackedWidget would be perfect. But in his initial code notice he show()s them both. He then says

                              and I want to be able to switch from one window to the other and back to the first

                              It took it to mean he wants to see both at the same time. But we can't tell.

                              @MortyMars Simple question: do you want both windows visible at same time --- QStackedWidget no good --- or do you only want one at a time --- in which case it's fine/recommended, but then don't start out show()ing both of them. So which is it?

                              MortyMarsM Offline
                              MortyMarsM Offline
                              MortyMars
                              wrote on last edited by
                              #14

                              @JonB and @SGaist :

                              In fact I have two different windows, mainly to display data masks that don't fit in a single window.
                              The two windows are the same size and overlap perfectly.
                              And I don't need to have both displayed at the same time (since they don't fit on the screen anyway).

                              Pl45m4P 1 Reply Last reply
                              0
                              • MortyMarsM MortyMars

                                @JonB and @SGaist :

                                In fact I have two different windows, mainly to display data masks that don't fit in a single window.
                                The two windows are the same size and overlap perfectly.
                                And I don't need to have both displayed at the same time (since they don't fit on the screen anyway).

                                Pl45m4P Offline
                                Pl45m4P Offline
                                Pl45m4
                                wrote on last edited by
                                #15

                                @MortyMars said in Object as global variable:

                                In fact I have two different windows, mainly to display data masks that don't fit in a single window.
                                The two windows are the same size and overlap perfectly.

                                I can't see how this works or why one window won't....

                                The two windows are the same size and overlap perfectly.
                                And I don't need to have both displayed at the same time (since they don't fit on the screen anyway).

                                Then you could go with a QStackedWidget as recommended by @SGaist .
                                You just simply replace / change the widget from one window instead of showing two windows which have a "static" widget.

                                (since they don't fit on the screen anyway).

                                I mean, this is no excuse ;-)
                                Since you are the developer, you can make them fit ;-)
                                There are plenty techniques to do so (layouts, scroll areas... etc).


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                MortyMarsM 1 Reply Last reply
                                1
                                • Pl45m4P Pl45m4

                                  @MortyMars said in Object as global variable:

                                  In fact I have two different windows, mainly to display data masks that don't fit in a single window.
                                  The two windows are the same size and overlap perfectly.

                                  I can't see how this works or why one window won't....

                                  The two windows are the same size and overlap perfectly.
                                  And I don't need to have both displayed at the same time (since they don't fit on the screen anyway).

                                  Then you could go with a QStackedWidget as recommended by @SGaist .
                                  You just simply replace / change the widget from one window instead of showing two windows which have a "static" widget.

                                  (since they don't fit on the screen anyway).

                                  I mean, this is no excuse ;-)
                                  Since you are the developer, you can make them fit ;-)
                                  There are plenty techniques to do so (layouts, scroll areas... etc).

                                  MortyMarsM Offline
                                  MortyMarsM Offline
                                  MortyMars
                                  wrote on last edited by
                                  #16

                                  @Pl45m4 said in Object as global variable:

                                  Then you could go with a QStackedWidget as recommended by @SGaist .

                                  I see that @SGaist's QStackedWidget solution is unanimously approved.
                                  So I'm going to take a closer look :-)

                                  (since they don't fit on the screen anyway).

                                  I mean, this is no excuse ;-)
                                  Since you are the developer, you can make them fit ;-)

                                  My only excuse is that I'm just a modest occasional developer ;-)
                                  But the way my application is organised makes ergonomic sense.

                                  Thanks for your help :-)

                                  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