1
votes

I have a problem with sortFilterProxyModel of QT's table model/view.

When I type the word to search from the table which is filled by model(ListModel) in QML than it sorted right and result is also accordingly (like correct name of the file and it's Id ) but when I clicked the row to get index, it shows from zero to display the content because of it's property of re-indexing but I don't want because of it's re-indexing I received wrong fileId as I set.

I am stuck with this problem. How can I leave it out?

2
Not sure if I understood you correctly, but there is: QSortFilterProxyModel::mapToSource(QModelIndex index) - Sebastian Lange
No not using this, because I am using Qml so the sortFilterProxyModel use directly there. and the method "QObject *SortFilterProxyModel::source() const //PNU { //qDebug()<<"sourceOfSortfileterProxyModel : "<<source(); return sourceModel(); }" calling there directly - vicky
How about this answer over here stackoverflow.com/questions/34252413/… ? I don't know what programming language do you use. The link is for python. But, you can have an idea. - mtb

2 Answers

2
votes

I face the same issue and got it to work by doing this:

In your sortfilterproxy.h add a public slot function:

public slots:
    int getModelIndex(int);

In your sortfilterproxy.cpp implement this function:

int SortFilterProxyModel::getModelIndex(int row)
{
     QModelIndex sourceIndex = mapToSource(this->index(row, 0));
    return sourceIndex.row();//this returns an integer
}

Now on your TableView in QML you can do this:

  onCurrentRowChanged: 
  {
   var currentIndex = currentRow;
   console.log(currentIndex);
   var rowModelIndex= proxyModel.getModelIndex(currentIndex);
   console.log(rowModelIndex);
...

Hope this helps.

Michael

1
votes

Caught the same problem as you. Playing around QtQuick TableView Example and Qt Blog note

I've found a QTBUG-50019 (TableView: Track selection by the item and not by the index of the item). Seems it's not going to be resolved soon.

As alternative you could create an additional index in your Table/List View and juggle them for self aims like this

ListView {
    id: root

    property real selectedIndex: -1
    ...
    delegate: Item {
            color: (index === root.selectedIndex) 
                   ? parent.color = "gray" 
                   : parent.color = root.color

            Connections {
                target: root

                onSelectedIndexChanged: {
                    if(index === root.selectedIndex)
                        root.currentIndex = index
                }
            }

            MouseArea {
                anchors.fill: parent
                onClicked: root.selectedIndex = index
            }
     }
}