如何在 QAbstractItemModel 和 QTreeView 类中查找 Subitem?
问题:如何在 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案如下:
*
如果没有该参数,match() 函数仅搜索 1 级Here is the solution:
*
Without that argument match() function searches only 1 level我在 QTreeView 上的工作示例。
My working example on QTreeView.
想要添加到 @mosg 给出的答案
第四个参数实际上是点击参数。
它决定了一个人想要返回多少场比赛。
可以看出,对于所有匹配指定 -1
此处:
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: