Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. dynamic Delegate component and use of DelegateModelGroup in DelegateModel
Forum Updated to NodeBB v4.3 + New Features

dynamic Delegate component and use of DelegateModelGroup in DelegateModel

Scheduled Pinned Locked Moved General and Desktop
delegatemodelgrdynamicdelegate
4 Posts 2 Posters 1.9k 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.
  • M Offline
    M Offline
    Mephi
    wrote on last edited by Mephi
    #1

    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.

    p3c0P 1 Reply Last reply
    0
    • M 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.

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

      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
      0
      • M Offline
        M Offline
        Mephi
        wrote on last edited by
        #3

        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

        p3c0P 1 Reply Last reply
        0
        • M Mephi

          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

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

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

          • Login

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