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. [SOLVED]How do I use a global varaible in qt

[SOLVED]How do I use a global varaible in qt

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

    I have a structure that is a linked list, and I need to use it in multiple windows. My question is how I can make the variable global or other method to used the variable in all the windows.
    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome
      Global vars are not the best design. It would be better to
      give the list as parameter to the other windows.
      Like
      mywindow.setlist( thelist );
      so each would have its own pointer to the list versus just having a global one.
      Anyway, to create a global list:
      create a mylist.h file and a mylist.cpp to contain your list.
      In the .h file
      extern list mylist;
      and in the cpp file
      mylist list;
      then include mylist.h in the other windows.
      and it will know mylist;

      T 1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Depending on what the list contains using a global for this might range from moderately bad idea to a horrible bug.

        Remember that you can't make global QObjects, as they should be born and die during the lifespan of QApplication object. So if the list contains QObjects it's a no-no.
        Design-wise there should be no state that belongs to no one, which is the case for global variables. The data should always belong to some object that will be responsible for cleaning it up when its time is due, otherwise you've got a curious case of questionable responsibility.

        So my advice is to make it a member of the class that feels best for this and expose it to others via an access method. Passing it to all the classes via parameter like @mrjj suggested is also an option (a good one in some cases), although from my experience the responsibility for the data tends to blur this way the deeper into class structure it gets passed. Qt has a nice tree-like dependency structure provided by its parent-child relations. If you can't find a suitable object to host the data in that tree, tie it to the root - the application object, but don't make it hang around rootless - it usually causes a bug or a maintenance headache down the line.

        1 Reply Last reply
        1
        • mrjjM mrjj

          Hi and welcome
          Global vars are not the best design. It would be better to
          give the list as parameter to the other windows.
          Like
          mywindow.setlist( thelist );
          so each would have its own pointer to the list versus just having a global one.
          Anyway, to create a global list:
          create a mylist.h file and a mylist.cpp to contain your list.
          In the .h file
          extern list mylist;
          and in the cpp file
          mylist list;
          then include mylist.h in the other windows.
          and it will know mylist;

          T Offline
          T Offline
          TLoZ_KZP
          wrote on last edited by
          #4

          @mrjj Well I tried give the list as parameter to the windows but when I do, it appears a lot of errors, and when I include the mylist.h dont recognize mylist.

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            well, if you post your code we can try to help.
            Also the errors if possible.

            If you are new to c++ , maybe try some simple stuff before trying with Qt ?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TLoZ_KZP
              wrote on last edited by
              #6

              Well now it's working, I only put "extern list mylist" below the structure of list

              mrjjM 1 Reply Last reply
              0
              • T TLoZ_KZP

                Well now it's working, I only put "extern list mylist" below the structure of list

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Super.
                Yes, the "extern list mylist" must be under the definition of "list" or the
                type (in this case list) is unknown for compiler and it gives errors.
                Should have mentioned that.
                Happy coding.

                1 Reply Last reply
                0
                • TheBadgerT Offline
                  TheBadgerT Offline
                  TheBadger
                  wrote on last edited by
                  #8

                  @TLoZ_KZP You can take a look at the singleton design pattern if you want a more OO approach to having a global variable.

                  The singleton is a highly debated pattern but sometimes it is the correct solution, in most situations it is better than global variables.

                  Although the best and recommended approach is as @mrjj pointed out: Pass the list to the objects that needs it through a function.


                  Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                  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