【问题标题】:QSortFilterProxyModel not showing 2nd column in QTreeViewQSortFilterProxyModel 未在 QTreeView 中显示第二列
【发布时间】: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


【解决方案1】:

我发现了问题...

我使用的是 QStyledItemDelegate,我从它派生了一个类并覆盖了绘制函数。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2018-11-06
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多