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

Impossible to use sections while using ObjectModel as a source of data for ListView

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Invalid
    • Affects Version/s: 5.0.0, 5.12.8
    • Fix Version/s: None
    • Labels:
      None
    • Platform/s:
      Windows

      Description

      It's unclear if there is a possibility to use ListView's sectioning feature with ObjectModel which can contain arbitrary qml items without 'roles'.

      I tried to create additional property for every item in the ObjectModel which could be used as a section name, but it didn't work:

      ListView {
          section.property: "sectionName"
          section.delegate: Text { text: section }
          
          model: ObjectModel {
              Rectangle {
                  property string sectionName: "first section"
                  width: parent.width
                  height: 50
              }
          }
      }
      

      I've looked into ListView's source code and found that it uses model's 'stringValue' method (qquicklistview.cpp#1205) in order to get section names for every item in the list. That method returns string which will be the section's name (qqmlobjectmodel.cpp#281):

      return QQmlEngine::contextForObject(d->children.at(index).item)->contextProperty(name).toString();

      It always returns empty strings by some reason, what is more, contextForObject returns the same context for all items in the model no matter which index used.

      I've replaced the line above with this one:

      return d->children.at(index).item->property(name.toLocal8Bit()).toString();

      As the result, I've got sections worked at least for my specific case (I'm not sure this would work if ObjectModel contained Loaders)

      But the initial question remains unanswered: is there a proper way to use sections with ObjectModels in ListView? And, if not, can my small change of  QQmlObjectModel::stringValue() be used as a solution?

       

        Attachments

          Issue Links

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

            Activity

              People

              Assignee:
              qt.team.quick.subscriptions Qt Quick and Widgets Team
              Reporter:
              danilpinchuk Danil Pinchuk
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes