Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
troyane opened this issue Apr 8, 2021 · 2 comments
Open

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

troyane opened this issue Apr 8, 2021 · 2 comments

Comments

@troyane
Copy link

troyane commented Apr 8, 2021

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.

@troyane
Copy link
Author

troyane commented Apr 18, 2021

@oKcerG any input on this?

@oKcerG
Copy link
Owner

oKcerG commented May 3, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants