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. [SOLVED] How to use multiple different list element in ListView
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to use multiple different list element in ListView

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.8k Views 2 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.
  • J Offline
    J Offline
    jimcad
    wrote on last edited by jimcad
    #1

    I can use different properties in ListElement when using QML model. The following code works properly. Arg2 is only in the first ListElement:

    ListView  {
        id:listView
        height: 200
        Layout.fillWidth: true
        delegate: Rectangle {
            height: 50
            Label {
                anchors.fill: parent
                text: arg1 + " " + (arg2 === undefined ? "" : " " + arg2)
            }
        }
        model: ListModel {
            ListElement { arg1: "Elem1"; arg2: "Elem1arg2" }
            ListElement { arg1: "Elem2" }
        }
    }
    

    But I'd like to load model from C++. I have multiple inherited classes.

    Here is base idea of my classes:

    class Class1 : public QObject
    {
      Q_PROPERTY(QString arg1...
    //...
    class Class2 : public Class1
      Q_PROPERTY(QStirng arg2...
    

    I tried to use QObjectList -based model. I didn't get it working because only properties from Class1 are available:

    QList<QObject*> list;
    list.append(new Class1("Elem1"));
    list.append(new Class2("Elem2","Elem2arg1" ));
    return QVariant::fromValue(list);
    

    How I can get properties from both classes? Or is it even possible?

    p3c0P 1 Reply Last reply
    0
    • J jimcad

      I can use different properties in ListElement when using QML model. The following code works properly. Arg2 is only in the first ListElement:

      ListView  {
          id:listView
          height: 200
          Layout.fillWidth: true
          delegate: Rectangle {
              height: 50
              Label {
                  anchors.fill: parent
                  text: arg1 + " " + (arg2 === undefined ? "" : " " + arg2)
              }
          }
          model: ListModel {
              ListElement { arg1: "Elem1"; arg2: "Elem1arg2" }
              ListElement { arg1: "Elem2" }
          }
      }
      

      But I'd like to load model from C++. I have multiple inherited classes.

      Here is base idea of my classes:

      class Class1 : public QObject
      {
        Q_PROPERTY(QString arg1...
      //...
      class Class2 : public Class1
        Q_PROPERTY(QStirng arg2...
      

      I tried to use QObjectList -based model. I didn't get it working because only properties from Class1 are available:

      QList<QObject*> list;
      list.append(new Class1("Elem1"));
      list.append(new Class2("Elem2","Elem2arg1" ));
      return QVariant::fromValue(list);
      

      How I can get properties from both classes? Or is it even possible?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @jimcad AFAIK it should work. All the parent's properties should be visible to child.
      Have you set the inherited property's value from child ?

      157

      J 1 Reply Last reply
      0
      • p3c0P p3c0

        @jimcad AFAIK it should work. All the parent's properties should be visible to child.
        Have you set the inherited property's value from child ?

        J Offline
        J Offline
        jimcad
        wrote on last edited by
        #3

        @p3c0 Yes, arg1 is set for both and it's visible in QML. But arg2 is available only in Class2 and it's not visible in QML. That is the problem.

        p3c0P 1 Reply Last reply
        0
        • J jimcad

          @p3c0 Yes, arg1 is set for both and it's visible in QML. But arg2 is available only in Class2 and it's not visible in QML. That is the problem.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          @jimcad OfCourse how would arg2 be available to Class1 ? Children inherit properties of their parents and not the other way.
          You can add a check there something like:

          Text {
              text : typeof model.modelData.arg2==="undefined" ? "DefaultValue" : model.modelData.arg2
          }
          

          157

          J 1 Reply Last reply
          0
          • p3c0P p3c0

            @jimcad OfCourse how would arg2 be available to Class1 ? Children inherit properties of their parents and not the other way.
            You can add a check there something like:

            Text {
                text : typeof model.modelData.arg2==="undefined" ? "DefaultValue" : model.modelData.arg2
            }
            
            J Offline
            J Offline
            jimcad
            wrote on last edited by jimcad
            #5

            @p3c0 Yes, I know that. There must be something wrong in my code because this not working either:

                MyClass1{ id: class1 }
                MyClass2{ id: class2 }
            
                Component.onCompleted: {
                    class2.arg1 = "Changed arg1"
                    console.log(class2.arg1) // works ok
                    class2.arg2 = "Changed arg2" // <-- Doesn't work        
                    console.log(class2.arg2)
                }
            

            Seems that all properties from inherited classes doesn't works. Here is my example project: https://www.dropbox.com/s/mi33b8ml8nctrup/qml-test.zip?dl=0

            J 1 Reply Last reply
            0
            • J jimcad

              @p3c0 Yes, I know that. There must be something wrong in my code because this not working either:

                  MyClass1{ id: class1 }
                  MyClass2{ id: class2 }
              
                  Component.onCompleted: {
                      class2.arg1 = "Changed arg1"
                      console.log(class2.arg1) // works ok
                      class2.arg2 = "Changed arg2" // <-- Doesn't work        
                      console.log(class2.arg2)
                  }
              

              Seems that all properties from inherited classes doesn't works. Here is my example project: https://www.dropbox.com/s/mi33b8ml8nctrup/qml-test.zip?dl=0

              J Offline
              J Offline
              jimcad
              wrote on last edited by
              #6

              @p3c0 I found it. It was missing Q_OBJECT macro in Class2. Now it works properly. Thanks for your advice.

              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