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

No way to support both single and multi-role models in delegates that use required properties

XMLWordPrintable

    • 566e5e1ac (dev), e9f650cad (dev), be884811d (dev)

      https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html says:

      Models that do not have named roles (such as the ListModel shown below) will have the data provided via the modelData role. The modelData role is also provided for models that have only one role. In this case the modelData role contains the same data as the named role.

      It then says:

      Note: model, index, and modelData roles are not accessible if the delegate contains required properties, unless it has also required properties with matching names.

      The problem with this is that e.g. ComboBox needs to support all kinds of models:

      T.ComboBox {
          id: control
      
          // ...
      
          delegate: ItemDelegate {
              // ...
              text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
              // ...
          }
      

      So if we declare required properties for model and modelData, we get an error (depending on what type of model is in use):

          qrc:/CustomComboBox.qml:8:15: QML Component: Cannot create delegate
          qrc:/CustomComboBox.qml:22:9: Required property modelData was not initialized
      

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

            ulherman Ulf Hermann
            mitch_curtis Mitch Curtis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes