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 7 Oct 2015, 11:07 last edited by jimcad 10 Aug 2015, 20:03
    #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?

    P 1 Reply Last reply 7 Oct 2015, 12:31
    0
    • J jimcad
      7 Oct 2015, 11:07

      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?

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 7 Oct 2015, 12:31 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 7 Oct 2015, 13:02
      0
      • P p3c0
        7 Oct 2015, 12:31

        @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 7 Oct 2015, 13:02 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.

        P 1 Reply Last reply 8 Oct 2015, 05:17
        0
        • J jimcad
          7 Oct 2015, 13:02

          @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.

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 8 Oct 2015, 05:17 last edited by p3c0 10 Aug 2015, 05:18
          #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 8 Oct 2015, 19:55
          0
          • P p3c0
            8 Oct 2015, 05:17

            @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 8 Oct 2015, 19:55 last edited by jimcad 10 Aug 2015, 19:56
            #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 8 Oct 2015, 20:02
            0
            • J jimcad
              8 Oct 2015, 19:55

              @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 8 Oct 2015, 20:02 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

              1/6

              7 Oct 2015, 11:07

              • Login

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