Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    dynamic Delegate component and use of DelegateModelGroup in DelegateModel

    General and Desktop
    delegatemodelgr dynamic delegate
    2
    4
    1593
    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.
    • M
      Mephi last edited by Mephi

      Hello,
      i have a simple test qmlproject, where i create a dynamically a delegate for a gridview, this delegate is used in a DelagetModel with groups, but in the delegate the groups are undefined :-(
      here is a short example code:

      Rectangle {
          id: root
          anchors.fill: parent
      
          property var trackObjectViewDelegate: Qt.createComponent("DelegateItem.qml", root)
      
          DelegateModel {
              id: visualModel
      
              model: ListModel {
                  ListElement { name: "blue" }
                  ListElement { name: "green" }
                  ListElement { name: "red" }
              }
      
              groups: [
                  DelegateModelGroup { name: "selected" }
              ]
      
              delegate: trackObjectViewDelegate
          }
      
          GridView {
              id: gridID
              anchors.fill: parent
      
              model: visualModel
      
              cellHeight: 300
              cellWidth: 300
          }
      }
      

      in the DelegateItem.qml i try to read the selected property like this:

      Rectangle {
          id: item
          height: 25
          width: 200
          Text {
              text: {
                  var text = "Name: " + name
                  if (item.DelegateModel.inSelected)
                      text += " (" + item.DelegateModel.selectedIndex + ")"
                  return text;
              }
          }
          MouseArea {
              anchors.fill: parent
              onClicked: item.DelegateModel.inSelected = !item.DelegateModel.inSelected
          }
      }
      

      It is not working, and i get following error:
      TypeError: Cannot read property 'inSelected' of undefined

      Does anybody know what to do?
      If i not create the delegate dynamically, then it is working perfect, but this is not an option.

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Mephi last edited by

        Hi @Mephi and Welcome,

        TypeError: Cannot read property 'inSelected' of undefined

        The error says it exactly. The element is indefined and thus cannot access that property. Also I fail to see property named inSelected.

        You can either use parent or directly use id of DelegateModel.

        157

        1 Reply Last reply Reply Quote 0
        • M
          Mephi last edited by

          Hello p3c0,

          thanks for your reply, i know that the property inSelected is undefined, thats the intension of my question ;-) , but why it is undefined?

          it has to be defined, because it is working if i not create the delegate dynamically.

          inSelected property is generated automatically
          http://doc.qt.io/qt-5/qml-qtqml-models-delegatemodel.html#groups-prop

          p3c0 1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators @Mephi last edited by

            @Mephi Hmm, sorry for that. I wasn't aware of it as i have never used it before. So digging in it seems that the attached properties of DelegateModel doesn't seem to work if the delegate is in separate file. Could be a bug I suppose. You can try reporting it here.

            157

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