Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Adding a ListElemente to another List

    QML and Qt Quick
    3
    4
    578
    Loading More Posts
    • 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
      AchimR last edited by

      I'm trying to add a ListElement to another List.
      The strange thing is that List2.count gets higher, but I can not put the name into the text. I hope you can help me.
      Here is the Code:

      import QtQuick 2.0
      
      Rectangle {
          width: 100
          height: 62
      
          ListModel {
              id: list1
              ListElement {
                      name: "Apple"
                      cost: 2.45
                  }
          }
          ListModel {
              id: list2
          }
          MouseArea {
                 anchors.fill: parent
                 onClicked: { parent.color = 'red'
                              list2.append(list1.get(0))
                 }
             }
          Text{
              text: list2.get(0).name
      
          }
      }
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        Your Text element gets it's content from the first element of list2 but as written here, changes to list2 won't affect the content of the Text element.

        You should rather use a ListView to show the content of your ListModel.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • ?
          A Former User last edited by A Former User

          Hi!

          text: list2.get(0).name

          First of all this produces a runtime error when your program starts because at that time list2 is empty thus list2.get(0) evaluates to undefined and list2.get(0).name tries to read a property of undefined.

          So you may ask: "But why isn't the text property updated once list2.get(0).name becomes valid? I mean, name is a property so we have a property binding!" The answer is: No, we don't have a property binding. The property binding is created on program start-up. As already mentioned at that time there is no name property to bind to because list2.get(0) is not an object. So your statement is not a property binding and will never be reevaluated.

          1 Reply Last reply Reply Quote 2
          • A
            AchimR last edited by

            ah okay, I get it.
            thanks for the explanation!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post