在过滤期间展开树状视图中的特定项目

时间:2019-06-26 21:31:05

标签: c++ qt qt5 qtreeview qsortfilterproxymodel

让我们考虑这个树数据:

Root
|-v A1
| |-- textA
|
|-v B1
| |-- textB

搜索“ A1”时,我希望A1项不展开(但可以展开以查看子项):

Root
|-> A1

搜索“ textA”时,我希望扩展A1项(以查看匹配的子项):

Root
|-v A1
| |-- textA

QSortFilterProxyModel提供的(标准)过滤效果很好,但是我找不到如何实现“在需要时扩展”的方法。

我尝试了一些愚蠢的代码,但是没有用:

bool MySortFilterProxyModel::filterAcceptsRow(
  int sourceRow,
  const QModelIndex &sourceParent
) const {
  bool result = QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);

  if (m_tree) {
  //   if (result)
       m_tree->expand(sourceParent);
  }
 return result;
}

2 个答案:

答案 0 :(得分:0)

我认为以下解决方案可能会对您有所帮助。

我认为这是扩展项目的明智方法,如果它们只是几个要搜索名称的孩子。如果一个人改变了搜索范围,一个未解决的问题可能是孩子的折叠。在搜索字段中输入例如C-0-0-0,以了解我的解决方案的行为。

#include <QApplication>
#include <QTreeView>
#include <QDebug>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>


int main(int argc, char **args)
{
    QApplication app(argc, args);
    auto frame = new QFrame;
    frame->setLayout(new QVBoxLayout);
    auto model = new QStandardItemModel;
    for (auto i=0; i<10; i++)
    {
        auto item = new QStandardItem(QString("A-%1").arg(i));
        model->appendRow(item);
        for (auto j=0; j<10; j++)
        {
            auto item2 = new QStandardItem(QString("B-%1-%2").arg(i).arg(j));
            item->appendRow(item2);
            for (auto k=0; k<10; k++)
            {
                auto item3 = new QStandardItem(QString("C-%1-%2-%3").arg(i).arg(j).arg(k));
                item2->appendRow(item3);
            }
        }
    }

    auto proxyModel = new QSortFilterProxyModel;
    proxyModel->setSourceModel(model);
    proxyModel->setRecursiveFilteringEnabled(true);
    auto view = new QTreeView;
    view->setModel(proxyModel);
    frame->layout()->addWidget(view);
    auto edit = new QLineEdit;
    frame->layout()->addWidget(edit);
    frame->show();
    QObject::connect(edit, &QLineEdit::textChanged, [&](auto test) {
        proxyModel->setFilterFixedString(test);
        Qt::MatchFlags flags;
        flags.setFlag(Qt::MatchFlag::MatchStartsWith, true);
        flags.setFlag(Qt::MatchFlag::MatchWrap, true);
        flags.setFlag(Qt::MatchFlag::MatchRecursive, true);
        auto indexList=proxyModel->match(proxyModel->index(0,0), Qt::ItemDataRole::DisplayRole, test, -1, flags);
        for (auto index : indexList)
        {
            auto expanderIndex = index.parent();
            while (expanderIndex.isValid())
            {
                view->expand(expanderIndex);
                expanderIndex = expanderIndex.parent();
            }

        }
        qDebug() << indexList.size();
    });
    app.exec();
}

改进的解决方案

以下改进的解决方案仅使用模型中找到的索引,而无需对match提供的索引进行不同的搜索。

main.cpp

#include <QApplication>
#include <QTreeView>
#include <QDebug>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include "MyProxyModel.h"

int main(int argc, char **args)
{
    QApplication app(argc, args);
    auto frame = new QFrame;
    frame->setLayout(new QVBoxLayout);
    auto model = new QStandardItemModel;
    for (auto i=0; i<10; i++)
    {
        auto item = new QStandardItem(QString("A-%1").arg(i));
        model->appendRow(item);
        for (auto j=0; j<10; j++)
        {
            auto item2 = new QStandardItem(QString("B-%1-%2").arg(i).arg(j));
            item->appendRow(item2);
            for (auto k=0; k<10; k++)
            {
                auto item3 = new QStandardItem(QString("C-%1-%2-%3").arg(i).arg(j).arg(k));
                item2->appendRow(item3);
            }
        }
    }

    auto proxyModel = new MyProxyModel;
    proxyModel->setSourceModel(model);
    proxyModel->setRecursiveFilteringEnabled(true);
    auto view = new QTreeView;
    view->setModel(proxyModel);
    frame->layout()->addWidget(view);
    auto edit = new QLineEdit;
    frame->layout()->addWidget(edit);
    frame->show();
    QObject::connect(edit, &QLineEdit::textChanged, [&](auto test) {
        proxyModel->setFilterFixedString(test);
        if (test == "") return;
        view->collapseAll();
        QList<QModelIndex> acceptedIndices = proxyModel->findIndices();
        for (auto index : acceptedIndices)
        {
            auto expanderIndex = index.parent();
            while (expanderIndex.isValid())
            {
                view->expand(expanderIndex);
                expanderIndex = expanderIndex.parent();
            }

        }
        qDebug() << acceptedIndices.size();
    });
    app.exec();
}

MyProxyModel.h

#pragma once

#include <QSortFilterProxyModel>

class MyProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT
public:
    QList<QModelIndex> findIndices() const
    {
        QList<QModelIndex> ret;
        for (auto iter=0; iter < rowCount(); iter++)
        {
            auto childIndex = index(iter, 0, QModelIndex());
            ret << recursivelyFindIndices(childIndex);
        }
        return ret;
    }


    bool rowAccepted(int source_row, const QModelIndex& source_parent) const
    {
        return filterAcceptsRow(source_row, source_parent);
    }
private:
    QList<QModelIndex> recursivelyFindIndices(const QModelIndex& ind) const
    {
        QList<QModelIndex> ret;
        if (rowAccepted(ind.row(), ind.parent()))
        {
            ret << ind;
        }
        for (auto iter=0; iter<rowCount(ind); iter++)
        {
            ret << recursivelyFindIndices(index(iter, 0, ind));
        }
        return ret;
    }
};

答案 1 :(得分:0)

对@ Aleph0答案的几点建议:

  1. main.cpp 件:
        view->collapseAll();
        QList<QModelIndex> acceptedIndices = proxyModel->findIndices();
        for (auto index : acceptedIndices)
        {
            auto expanderIndex = index.parent();
            while (expanderIndex.isValid())
            {
                view->expand(expanderIndex);
                expanderIndex = expanderIndex.parent();
            }

        }

为了防止重复扩展,最好检查 expanderIndex 是否已经扩展并停止上升。在最坏的情况下,它将使您免受 O(n ^ 2)复杂性的困扰(应该是线性的)。

  1. 以类似的方式,可以实现MyProxyModel::findIndices方法而无需递归来加快处理速度(但是,现代编译器应该足够聪明才能展开它)