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. Adding a ListElemente to another List

Adding a ListElemente to another List

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 777 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.
  • A Offline
    A Offline
    AchimR
    wrote on 28 Jan 2016, 20:33 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Jan 2016, 21:23 last edited by
      #2

      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
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on 28 Jan 2016, 21:54 last edited by A Former User
        #3

        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
        2
        • A Offline
          A Offline
          AchimR
          wrote on 28 Jan 2016, 23:12 last edited by
          #4

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

          1 Reply Last reply
          0

          1/4

          28 Jan 2016, 20:33

          • Login

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