过滤期间,在树视图中扩展特定项目



让我们考虑一下此树数据:

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;
}

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

我认为,如果他们只是搜索名称的孩子,那么他们就会是扩展物品的理智方式。如果一个人更改搜索字段,则尚未解决的问题可能是儿童的折叠。例如在搜索字段中键入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;
    }
};

@aleph0的一些建议答案:

  1. main.cpp pice:
        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方法可以实现而无需递归以加快速度(但是,现代编译器应该足够聪明即可展开它(

相关内容

  • 没有找到相关文章