Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML: C++ Model with different (derived) objects. Is it possible?
QtWS25 Last Chance

QML: C++ Model with different (derived) objects. Is it possible?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 5 Posters 960 Views
  • 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I have a QList<QObject*> which I use as a model in QML. The objects I put there have some Q_PROPERTY. So lets assume we have a class MarkGUI and a derived class ProtocolMarkGui with some additional properties. Is it possible to mix MarkGui and ProtocolMarkGui objects in the QList I use as model, and use different delegates for the different objects?

    sierdzioS 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a QList<QObject*> which I use as a model in QML. The objects I put there have some Q_PROPERTY. So lets assume we have a class MarkGUI and a derived class ProtocolMarkGui with some additional properties. Is it possible to mix MarkGui and ProtocolMarkGui objects in the QList I use as model, and use different delegates for the different objects?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @maxwell31 said in QML: C++ Model with different (derived) objects. Is it possible?:

      Is it possible to mix MarkGui and ProtocolMarkGui objects in the QList I use as model,

      Yes. QList<MarkGui*> will accept both MarkGui and ProtocolMarkGui objects. And QML won't complain, either.

      and use different delegates for the different objects?

      That's possible. Let's assume you have some property called isProtocol which returns true in ProtocolMarkGui but false in MarkGui. Then:

      delegate: Loader {
        source: "qrc:/" + isProtocol? "ProtocolDelegate.qml" : "Delegate.qml"
      }
      

      You should be able to get at your model data even inside the loader. If not, then try with modelData property, or pass the data via properties.

      There are other ways to do it, too - all depends on what kind of delegates you need.

      (Z(:^

      1 Reply Last reply
      2
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        I seem to have a problem with getting the properties of the derived class into to loader, while the base class works well in the loader. I do it the following way:

                delegate: Loader { // loader choosing the right component
                    property double timeStart: model.timeStart
                    property string timeEnd: model.timeEnd
                    // but the property from the derived class
                    property string derivedClassProperty: model.test // does not work
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          shaan7
          wrote on last edited by
          #4

          It'll be easier to debug if you can share a minimal code sample for this (maybe a repo on GitHub or something).

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            what is model ? what is test ? how did you expose the model to QML ? Please show your class hierarchy as well & properties you have defined.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              The property for accessing model data is called modelData, not model. Perhaps that is the issue.

              (Z(:^

              1 Reply Last reply
              1
              • M Offline
                M Offline
                maxwell31
                wrote on last edited by
                #7

                I will post a minimal example, might need some time though

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  Actually, you don't need anything before the property name

                  property double timeStart: timeStart
                  property string timeEnd: timeEnd
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1

                  • Login

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