【发布时间】:2016-02-13 06:29:29
【问题描述】:
我从 QSortFilterProxyModel 继承了一个类,以支持在我的代码中过滤分层树。
我在下面添加了我所做的代码。过滤完成后,第二列的数据不显示...
我不明白为什么会这样......
谁能帮我解决这个问题?
另外,当过滤完成时,树被折叠...我想在过滤完成时在树上调用expandAll。在我知道过滤完成的地方是否发出了一些信号或调用了一些函数?
类声明
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:
};
用法:
_proxyModel = new MySortFilterProxyModel(this);
_proxyModel->setFilterKeyColumn(-1);
_proxyModel->setSourceModel(_directiveTreeModel);
定义:
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;
}
输出:(第二列数据丢失)
【问题讨论】:
-
我发现它没有出现是因为:_dTreeView->setItemDelegateForColumn(1, delegate).. 但是,我不知道解决方案。请提出建议。
标签: c++ qt4 qtreeview qsortfilterproxymodel