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. Adding derived objects to a list dynamically
Qt 6.11 is out! See what's new in the release blog

Adding derived objects to a list dynamically

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

    Hi ...
    I have a situation:
    I have a abstract class from QObject and base class which is derivated from the abstract. There are approximately 30 classes which are derivated from the base class. These classes are all singleton and they will be constructed within project. I want to have list of these singleton objects always. I don't want to make hard code instead i want to save and access them from base class. I have tried to create a static list in base class and add objects to this list in constructor of base class but it fails and gives and error. I'm not much expert at object oriented design with combination of static members! If someone can help me i'll be appreciated.

    If i can have a static method to return list of derivated objects, that would be great but i don't know how?! May be it's one of design patterns which i don't know...

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      I don't think the problem is that static.
      So how are you creating the list?
      I ask this because you can't have a QList<QObject> for two reasons: QObject and the classes derived from QObject are not copyable, and second reason is that objects are not polymorphic (if you copy a Derived object into a Base object you slice off the Derived part), to solve both the problems you must use a QList of pointers to your objects: QList<QObject*>

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alizadeh91
        wrote on last edited by
        #3

        Ok, Can i add item's pointer in constructor of base class? then how?

        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