2016-03-20 3 views
2

私は最初の列(index0)に基づいて階層的なフィルタQTreeViewをフィルタリングする動作フィルタ機能(filterAcceptsRow)を持っています。ユーザーがトラフ(フィルタリングされた)QTreeViewを検索できるように、検索を接続する必要があります。QLineEditこの関数に検索アルゴリズムを追加する方法がわかりません。誰かが私を理解するのを助けることができますか?検索アルゴリズムは、すべての5つの列(index0-index4)でQStringを検索する必要があります。Qt(C++)でトラフa(フィルタリングされた)QSortFilterProxyModelを検索するには?

マイフィルタ機能:

bool ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const 
{ 
    QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent); 
    QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); 
    QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent); 
    QModelIndex index3 = sourceModel()->index(sourceRow, 3, sourceParent); 
    QModelIndex index4 = sourceModel()->index(sourceRow, 4, sourceParent); 

    if (m_filterEnabled) 
    { 
     foreach (const QString &row, rows) 
     { 
      if (sourceModel()->data(index0).toString().contains(row) && m_shownRow) 
       return true; //element should be shown 
      else if (sourceModel()->data(index0).toString().contains(row) && !m_shownRow) 
       return false; //element should NOT be shown 
     } 
     if (m_shownRow) 
      return false; 
     else 
      return true; 
    } else { 
     return true; //no filter -> show everything 
    } 
} 

答えて

2

最良の方法は、チェーンに2 proxymodelsです。

私はtableと似たようなことをしましたが、ツリーの場合も同じように動作します。

あなたはQSortFilterProxyModelから派生クラスを作成し、filterAcceptsRow(正規表現の使用方法と、ここでの例)を実装:

bool QubyxSearchFilterProxyModel::filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const 
{ 
    for(int i = 0; i < sourceModel()->columnCount(); i ++) 
    { 
     QModelIndex index = sourceModel()->index(sourceRow, i, sourceParent); 
     if(sourceModel()->data(index).toString().toLower().trimmed().contains(filterRegExp())) 
      return true; 
    } 

    return false; 
} 

正規表現あなたが検索LINEEDITの変更を処理し、あなたのスロットのいずれかに設定することができます。

QRegExp regExp(widget->lineEditSearch->text().toLower(),Qt::CaseSensitive,QRegExp::FixedString); 
searchProxyModel_->setFilterRegExp(regExp); 

、ツリーのコードで:

searchProxyModel_->setSourceModel(model); 
proxyModel_->setSourceModel(searchProxyModel_); 
+0

ありがとうございました、これは私の問題を解決しました! :) – Engo

関連する問題