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. Access variables between multiple classes/objects
Forum Updated to NodeBB v4.3 + New Features

Access variables between multiple classes/objects

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 818 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.
  • A Offline
    A Offline
    AaronKelsey
    wrote on last edited by
    #1

    I have 3-4 classes that need to all share some variables between each other. I wasn't sure which approach to take but I have thought of a couple of ideas. Is there a way to connect all 3 classes to one so that they can all access their protected/private variables? or would it be best to create a class that has the purpose of storing these variables? If so, how would I then pass the instance of that object between multiple classes?

    K 1 Reply Last reply
    0
    • A AaronKelsey

      I have 3-4 classes that need to all share some variables between each other. I wasn't sure which approach to take but I have thought of a couple of ideas. Is there a way to connect all 3 classes to one so that they can all access their protected/private variables? or would it be best to create a class that has the purpose of storing these variables? If so, how would I then pass the instance of that object between multiple classes?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @AaronKelsey

      Sounds like a typical example for inheritance https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

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

      A 1 Reply Last reply
      1
      • K koahnig

        @AaronKelsey

        Sounds like a typical example for inheritance https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

        A Offline
        A Offline
        AaronKelsey
        wrote on last edited by
        #3

        @koahnig It isn't inheritance, the classes do not have a "is-a" relationship. They're pages which share user entered information. I should have been more specific in my original post.

        J.HilkJ 1 Reply Last reply
        0
        • A AaronKelsey

          @koahnig It isn't inheritance, the classes do not have a "is-a" relationship. They're pages which share user entered information. I should have been more specific in my original post.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @AaronKelsey
          techenically it can be done with inheritance, but that would also requiere singletons and stuff, urgh let's not go there.

          The cleanest way I can see for this special case, is make a class/struct that holds all the data you want to "share" make the variables accessable via getters and setters, if your class is derived from QObject, you could emit a "changed"signal to update the other classes.

          Than in the top most parent class create that class + all of the other 4 and pass a reference around and store the pointer in a member variable


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          4

          • Login

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