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

ComponentBehavior: Bound and generic delegates don't mix well

    XMLWordPrintable

Details

    • e9f650cad (dev), be884811d (dev)

    Description

      I wanted to create a custom ComboBox, following the customization example:

      import QtQuick.Controls
      
      ComboBox {
          id: control
          delegate: MenuItem {
              width: ListView.view.width
              text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
              highlighted: control.highlightedIndex === index
          } 
      }

      This works with both a ListMode and a QVariantList exported from C++ as a model, but when I run this through the QML compiler, it tells me:

      Warning: IconComboBox.qml:7:15: Unqualified access [unqualified]
              text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
                    ^^^^^^^
      Info: Set "pragma ComponentBehavior: Bound" in order to use IDs from outer components in nested components.
      

      Fair enough: I addde a pragma ComponentBehavior: Bound to this file and I also added the (now needed) required properties for index and modelData. Now the array / QVariantList based model works, but the ListModel based ComboBox complains at runtime:

      qml: QML Component: Cannot create delegate
      qml: Required property modelData was not initialized (at qrc:/Mobile/IconComboBox.qml, 12)
      

      And I get an empty popup. if I require model instead of modelData, it's the other way around.

      Is there a better solution besides ignoring the qml compiler's complaint about the missing bound behavior pragma to write a generic delegate that can handle both model and modelData?

      Attachments

        Issue Links

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

          Activity

            People

              ulherman Ulf Hermann
              rgriebl Robert Griebl
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes