Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-114488

Modernize the DelegateModel/Package/DelegateModelGroup API

    XMLWordPrintable

Details

    Description

      Exhibit A

      Code (adapted from the official example at https://doc.qt.io/qt-6/qml-qtqml-models-delegatemodel.html#groups-prop )

      import QtQuick
      import QtQuick.Controls.Basic
      import QtQml.Models
      
      Window {
          width: 640
          height: 480
          visible: true
      
          ListView {
              anchors.fill: parent
              model:     DelegateModel {
                  model: ListModel {
                      ListElement { name: "Apple" }
                      ListElement { name: "Orange" }
                  }
      
                  groups: [
                      DelegateModelGroup { name: "selected" }
                  ]
      
                  delegate: Button {
                      id: item
                      width: 200
      
                      required property string name
      
                      text: {
                          let text = "Name: " + item.name
                          if (item.DelegateModel.inSelected)
                              text += ` (${item.DelegateModel.selectedIndex})`
                          return text
                      }
                      onClicked: item.DelegateModel.inSelected = !item.DelegateModel.inSelected
                  }
              }
          }
      }
      

       

      Compiler output

      Warning: Main.qml:30:29: Member "inSelected" not found on type "QQmlDelegateModelAttached" [missing-property]
                          if (item.DelegateModel.inSelected)
                                                 ^^^^^^^^^^
      Warning: Main.qml:30:29: Could not compile binding for text: Cannot load property inSelected from QQmlDelegateModelAttached of QQmlDelegateModelAttached. [compiler]
                          if (item.DelegateModel.inSelected)
                                                 ^^^^^^^^^^
      Warning: Main.qml:34:68: Member "inSelected" not found on type "QQmlDelegateModelAttached" [missing-property]
                      onClicked: item.DelegateModel.inSelected = !item.DelegateModel.inSelected
                                                                                     ^^^^^^^^^^
      Warning: Main.qml:34:68: Could not compile binding for onClicked: Cannot load property inSelected from QQmlDelegateModelAttached of QQmlDelegateModelAttached. [compiler]
                      onClicked: item.DelegateModel.inSelected = !item.DelegateModel.inSelected
                                                                                     ^^^^^^^^^^
      

       

      Exhibit B
      Code (adapted from the official example at https://doc.qt.io/qt-6/qml-qtqml-models-delegatemodel.html#parts-prop )

      import QtQuick
      import QtQml.Models
      
      Window {
          width: 640
          height: 480
          visible: true
      
          DelegateModel {
              id: visualModel
              delegate: Package {
                  id: del
      
                  required property string name
      
                  Text {
                      Package.name: "list"
                      text: del.name
                  }
              }
              model: ListModel {
                  ListElement { name: "Apple" }
                  ListElement { name: "Orange" }
              }
          }
      
          ListView {
              anchors.fill: parent
              model: visualModel.parts.list
          }
      }
      

       

      Compiler output

      Warning: Main.qml:29:28: Member "list" not found on type "QObject" [missing-property]
              model: visualModel.parts.list
                                       ^^^^
      Warning: Main.qml:29:28: Could not compile binding for model: Cannot load property list from QObject of QQmlDelegateModel::parts with type QObject. [compiler]
              model: visualModel.parts.list
                                       ^^^^
      

       

      Problems with current implementation

      • The API is not tooling-friendly (e.g. the tools cannot understand `item.DelegateModel.inSelected` or `visualModel.parts.list`)
      • The API is not very QML-ish
        • It uses pseudo attached properties that cannot be bound to. The official documentation says, "Warning: In contrast to normal attached properties, those [of DelegateModel] cannot be set in a declarative way."
        • DelegateModelGroup relies on many imperative functions. Furthermore, many of these functions take var and jsdict arguments.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              skoh-qt Sze Howe Koh
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes