如何在 QAbstractItemModel 和 QTreeView 类中查找 Subitem?

发布于 2024-09-11 06:52:51 字数 1360 浏览 1 评论 0原文

问题:如何在 QTreeView 加载 QAbstractItemModel 模型中使用 model->match() 查找子项方法?

问题model->match()找不到子项,wtf?!

以下是示例:

“替代文本”

正如您从图片中看到的,我正在尝试使用以下代码扩展 Layouts 子项:

void Dialog::restoreState(void)
{
    // get list
    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("MainWindow");
    QStringList List = settings.value("ExpandedItems").toStringList();
    settings.endGroup();

    foreach (QString item, List)
    {
        if (item.contains('|'))
            item = item.split('|').last();
        // search `item` text in model
        QModelIndexList Items = model->match(model->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
        if (!Items.isEmpty())
        {
            // Information: with this code, expands ONLY first level in QTreeView
            view->setExpanded(Items.first(), true);
        }
    }
}

其中 settings.ini 文件包含:

[MainWindow]
ExpandedItems=Using Containers, Connection Editing Mode, Form Editing Mode, Form Editing Mode|Layouts

PS:根项目在启动时成功扩展!

Question: how to find sub item, in a QTreeView loaded QAbstractItemModel model with model->match() method?

Problem: model->match() can't find sub items, wtf?!

Here is the example:

alt text

As you can see from the picture, I'm trying to expand Layouts sub item with this code:

void Dialog::restoreState(void)
{
    // get list
    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("MainWindow");
    QStringList List = settings.value("ExpandedItems").toStringList();
    settings.endGroup();

    foreach (QString item, List)
    {
        if (item.contains('|'))
            item = item.split('|').last();
        // search `item` text in model
        QModelIndexList Items = model->match(model->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
        if (!Items.isEmpty())
        {
            // Information: with this code, expands ONLY first level in QTreeView
            view->setExpanded(Items.first(), true);
        }
    }
}

Where settings.ini file contains:

[MainWindow]
ExpandedItems=Using Containers, Connection Editing Mode, Form Editing Mode, Form Editing Mode|Layouts

PS: root items successfully expands on start!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各空 2024-09-18 06:52:51

解决方案如下:

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            2, // look *
            Qt::MatchRecursive); // look *
  • * 如果没有该参数,match() 函数仅搜索 1 级

Here is the solution:

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            2, // look *
            Qt::MatchRecursive); // look *
  • * Without that argument match() function searches only 1 level
云裳 2024-09-18 06:52:51

我在 QTreeView 上的工作示例。

QModelIndexList Indexes = this->ui->treeView->selectionModel()->selectedIndexes();
if(Indexes.count() > 0)
{
    QStandardItemModel *am = (QStandardItemModel*)this->ui->treeView->model();

    QStack<QModelIndex> mis;
    QModelIndex mi = Indexes.at(0);
    while(mi.isValid())
    {
        mis.push(mi);
        mi = mi.parent();
    }

    QStandardItem *si;
    bool FirstTime = true;
    while (!mis.isEmpty())
    {
        mi = mis.pop();
        if(FirstTime)
        {
            FirstTime = false;
            si = am->item(mi.row());
        }
        else
        {
            si = si->child(mi.row());
        }
    }
  // "si" - is selected item
}

My working example on QTreeView.

QModelIndexList Indexes = this->ui->treeView->selectionModel()->selectedIndexes();
if(Indexes.count() > 0)
{
    QStandardItemModel *am = (QStandardItemModel*)this->ui->treeView->model();

    QStack<QModelIndex> mis;
    QModelIndex mi = Indexes.at(0);
    while(mi.isValid())
    {
        mis.push(mi);
        mi = mi.parent();
    }

    QStandardItem *si;
    bool FirstTime = true;
    while (!mis.isEmpty())
    {
        mi = mis.pop();
        if(FirstTime)
        {
            FirstTime = false;
            si = am->item(mi.row());
        }
        else
        {
            si = si->child(mi.row());
        }
    }
  // "si" - is selected item
}
日久见人心 2024-09-18 06:52:51

想要添加到 @mosg 给出的答案

第四个参数实际上是点击参数。
它决定了一个人想要返回多少场比赛。

可以看出,对于所有匹配指定 -1
此处

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            -1, // any number of hits
            Qt::MatchRecursive); // look *

Wanted to add to the answer that @mosg gave

The forth parameter is actually the hits parameters.
It decides ho many matches one wants to return.

For all matches specify -1 as can be seen
here:

QModelIndexList Items = model->match(
            model->index(0, 0),
            Qt::DisplayRole,
            QVariant::fromValue(item),
            -1, // any number of hits
            Qt::MatchRecursive); // look *
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文