2016-02-13 67 views
-1

我从QSortFilterProxyModel继承了一个类来支持我的代码中的分层树的过滤。QSortFilterProxyModel没有显示QTreeView中的第二列

我在下面添加了我所做的代码。过滤完成后,在第二列中的数据没有显示...

我无法理解这是为什么......

谁能帮助我吗?

此外,当过滤完成后,树被折叠......我想在过滤完成时调用树上的全部扩展。是否有一些信号发出或某些函数被调用,我知道过滤已完成?

类声明

class MySortFilterProxyModel : public QSortFilterProxyModel 
{ 
    Q_OBJECT 

public: 
    MySortFilterProxyModel(QObject *parent = 0); 

protected: 
    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; 
    bool ShowThis(const QModelIndex index) const; 

private: 
}; 

USAGE:

_proxyModel = new MySortFilterProxyModel(this); 

    _proxyModel->setFilterKeyColumn(-1); 
    _proxyModel->setSourceModel(_directiveTreeModel); 

DEFINITION:

bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, 
     const QModelIndex &sourceParent) const 
{ 
    QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); 
    return ShowThis(index); 
} 

bool MySortFilterProxyModel::ShowThis(const QModelIndex index) const 
{ 
    bool retVal = false; 
    //Gives you the info for number of childs with a parent 
    if (sourceModel()->rowCount(index) > 0) 
    { 
     for(int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++) 
     { 
      QModelIndex childIndex = sourceModel()->index(nChild,0,index); 
      if (! childIndex.isValid()) 
       break; 
      retVal = ShowThis(childIndex); 
      if (retVal) 
      { 
       break; 
      } 
     } 
    } 
    else 
    { 
     QModelIndex useIndex0 = sourceModel()->index(index.row(), 0, index.parent()); 
     QString name = sourceModel()->data(useIndex0, Qt::DisplayRole).toString(); 
     QModelIndex useIndex1 = sourceModel()->index(index.row(), 1, index.parent()); 
     QString value = sourceModel()->data(useIndex1, Qt::DisplayRole).toString(); 
     std::cout << "name : " << name.toStdString() << ", value : " << value.toStdString() << "\n";// , filterRegExp : " << filterRegExp() << "\n"; 
     if ((name.contains(filterRegExp()) || value.contains(filterRegExp()))) 
     { 
      retVal = true; 
     } 
     else 
      retVal = false; 
    }`enter code here` 
    return retVal; 
} 

OUTPUT:(在第二列中的数据丢失)

enter image description here

+0

我知道它不是因为: _dTreeView-> setItemDelegateForColumn(1,delegate).. 但是,我不知道解决方案。请建议。 –

回答

0

我想通了这个问题...

我用QStyledItemDelegate,我已经从它派生的类和重写油漆功能。

在疼痛功能中,我指的是我的原始模型。但是,在过滤的情况下,该模型为NULL,我应该使用我创建的代理模型。

相关问题