Skip to content

SortFilterProxyModel as a model for TableView (QtQuick 2.14+) #88

@troyane

Description

@troyane

Hello @oKcerG!

What will be your best bet for usage SortFilterProxyModel with TableView directly (since SortFilterProxyModel is subclass of QAbstractItemModel)?

Expecting to have something like:

import QtQuick 2.14
import QtQuick.Window 2.14
import SortFilterProxyModel 0.1

Window {
  visible: true
  width: 640
  height: 480
  title: qsTr("SortFilterProxyModel and TableView")

  ListModel {
    id: fruitModel
    ListElement { name: "Banana"; cost: 5 }
    ListElement { name: "Apple";  cost: 9 }
    ListElement { name: "Orange"; cost: 3 }
  }

  SortFilterProxyModel {
    id: sfpm
    sourceModel: fruitModel
  }

  TableView {
    anchors.fill: parent
    columnSpacing: 1
    rowSpacing: 1
    clip: true
    model: sfpm

    delegate: Rectangle {
      implicitWidth: 100
      implicitHeight: 50
      Text {
        text: display // "name" or "cost"
      }
    }
  }
}

If I override QQmlSortFilterProxyModel::data I can see it is executed only for indices:

  • QModelIndex(0,0,...)
  • QModelIndex(1,0,...)
  • QModelIndex(2,0,...)
    with a different roles numbers (e.g. 1 for cost, 0 for name). But never ask for second column (e.g. index QModelIndex(0,1,...)) even if I override columnCount to return hardcoded value 2.

Can you help me to deal with it? Or can you point on the methods that I need to override to make it work properly?

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions