oKcerG / SortFilterProxyModel

A nicely exposed QSortFilterProxyModel for QML
MIT License
298 stars 101 forks source link

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

Open troyane opened 3 years ago

troyane commented 3 years ago

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:

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.

troyane commented 3 years ago

@oKcerG any input on this?

oKcerG commented 3 years ago

Hello, You would need an actual table model (a QAbstractItemModel with multiple columns) as a source model to be able to use SFPM as the model of a TableView. TableModel instead of ListModel for example.

However SortFilterProxyModel only reads the data from the first column for now for sorting and filtering. One would need to do some modifications to filter and sort on multiple columns.