2016-07-20 106 views
0

我有QT的表模型/视图的sortFilterProxyModel问题。在qt中使用qml模型的SortFilterProxyModel

当我从QML中的模型(ListModel)填充的表格中搜索的词比它排序的权限和结果也相应(如文件的正确名称,它是Id),但当我点击行获取索引,它显示从零显示的内容,因为它是重新索引的属性,但我不想因为它的重新索引我收到错误fileId我设置。

我被这个问题困住了。我如何离开它?

+0

不知道如果我理解正确的你,但是有:'QSortFilterProxyModel :: mapToSource(QModelIndex指数)' –

+0

无不是用这个,因为我用这么设为Qml的sortFilterProxyModel使用直接出现。和方法 “的QObject * SortFilterProxyModel ::源()const的// PNU {// qDebug()<<” sourceOfSortfileterProxyModel: “<<源(); 回报sourceModel();} ” 直接调用有 – vicky

+0

如何关于这个答案在这里http://stackoverflow.com/questions/34252413/how-to-create-a-filter-for-qtablewidget/34286827#34286827? 我不知道你用什么编程语言。该链接是为python。但是,你可以有一个想法。 – mtb

回答

1

抓到了和你一样的问题。 玩耍 QtQuick的TableView例Qt Blog note

我发现一个QTBUG-50019(TableView中:曲目选择的项目,而不是由项目的索引)。 似乎它不会很快解决。

作为替代自我旨在这样

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 
      } 
    } 
} 
+0

谢谢@Alex Makarov,为您提供建议。但我确实通过分配与fileId列相同的角色来解决这个问题,所以现在每当我点击按钮打开文件时,它会提取正确的fileId,现在我不需要得到它的索引 – vicky

+0

您应该添加解决方案作为更新原始问题并让这个话题对其他人更有用。 –

2

我面临着同样的问题,你可以在你的表/列表视图中创建一个额外的指标,兼顾他们得到它做这个工作:

在你sortfilterproxy.h添加一个公共槽函数:

public slots: 
    int getModelIndex(int); 

在你sortfilterproxy.cpp实现这个功能:

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

现在对你的TableView在QML,你可以这样做:

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

希望这有助于。

迈克尔

+0

谢谢,这对我很有用,但我很困惑为什么这是必要的。 'mapToSource(index)'应该提供相同的功能,但不适用于我。 –