PyQT 崩溃(底层 C/C++ 对象已被删除)“之后”清除 QTreeWidget

发布于 2025-01-04 13:48:35 字数 694 浏览 2 评论 0原文

我尝试为 QTreeWidget 构建实时搜索,这意味着我有一个 QLineEdit,当我输入时,我将过滤 QTreeWidget 中显示的结果。

目前我用 QTreeWidget.findItems() 得到“仍然显示的项目”,

foundItems  = mainForm.ui.treeShips.findItems(text,QtCore.Qt.MatchContains)

稍后我清除 QTreeWidget ( mainForm.ui.treeShips)

mainForm.ui.treeShips.clear()

然后我想添加“仍然显示的项目”,

mainForm.ui.treeShips.addTopLevelItems(foundItems)

但随后程序崩溃了“底层 C/C++ 对象已被删除”错误

我的第一个想法是,通过清除 treeShips,foundItems 的内容也被清除,导致foundItems ( print(foundItems) ) 的对象和treeShips 的对象具有相同的地址,当程序想要使用此列表时,它会崩溃。

有人知道如何解决吗 或者有更好的想法来应用这样的“搜索过滤器”?

(我希望我能正确描述我的问题:))

谢谢并致以最诚挚的问候

i try to build a realtime search for a QTreeWidget, that means that i have a QLineEdit and when im typing, i will filter the shown results in the QTreeWidget.

At the moment i get the "still shown itmes" with QTreeWidget.findItems()

foundItems  = mainForm.ui.treeShips.findItems(text,QtCore.Qt.MatchContains)

later i clear the QTreeWidget ( mainForm.ui.treeShips) with

mainForm.ui.treeShips.clear()

then i want to add the "still shown items" with

mainForm.ui.treeShips.addTopLevelItems(foundItems)

but then the programm crashes with the "underlying C/C++ object has been deleted" error

My first idea is, that with clearing the treeShips, the content of foundItems got cleared too cause the objects of foundItems ( print(foundItems) ) and the objects of treeShips have the same adresses and when the programm want to work with this list, it crash.

Does someone know how to fix it,
or have a better idea to apply such a "search filter" ?

( i hope i could describe my problem correctly :) )

thanks and best regards

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

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

发布评论

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

评论(2

摇划花蜜的午后 2025-01-11 13:48:35

我找到了基于 Neox 的第二个想法的解决方法。

我迭代树中的所有项目并将隐藏标志设置为 true ( setHidden(True) )
然后我迭代foundItems并将隐藏标志设置为false(我认为这有效,因为两个列表具有相同的地址)

for item in mainForm.ui.treeShips.findItems("",QtCore.Qt.MatchContains):
    item.setHidden(True)

for item in foundItems:
    item.setHidden(False)

但我不知道这是否不是一种肮脏的方式,但它有效:)

我希望这会帮助其他人有同样问题的人。

i found a workaround based on the 2nd idea from Neox.

I iterate all items in the tree and set the hidden flag on true ( setHidden(True) )
and then i iterate the foundItems and set the hidden flag on false ( i think that works, cause both lists have the same adresses )

for item in mainForm.ui.treeShips.findItems("",QtCore.Qt.MatchContains):
    item.setHidden(True)

for item in foundItems:
    item.setHidden(False)

But i dont know if that isnt a dirty way, but it works :)

I hope that this will help other ppl who have the same problem.

涙—继续流 2025-01-11 13:48:35

每当您在 QTreeWidgetItem 上调用 clear 函数时,项目都会被删除,因此您得到的错误并不奇怪。您可以在以下 QTreeWidget 实现摘录中看到它:

void QTreeWidget::clear()
{
    Q_D(QTreeWidget);
    selectionModel()->clear();
    //the treeModel here is QTreeModel, refer to snippet below
    d->treeModel()->clear();
}


void QTreeModel::clear()
{
    SkipSorting skipSorting(this);
    for (int i = 0; i < rootItem->childCount(); ++i) {
        QTreeWidgetItem *item = rootItem->children.at(i);
        item->par = 0;
        item->view = 0;
        delete item;
    }
....
}

给您关于搜索实现的实际问题。首先这里是一个很好的代码片段,用于实现实际的搜索功能(不是真实的)时间)。 (抱歉,它是 C++,但我希望它没问题)。

要执行实时搜索,您必须将 void textChanged(const QString & text) 信号从 QLineEdit 连接到执行搜索的槽。

我希望能回答你的问题

Whenever you call clear function on a QTreeWidgetItem the items are deleted so the error you get is no surprise. You can see it in the following excerpt of the QTreeWidget implementation:

void QTreeWidget::clear()
{
    Q_D(QTreeWidget);
    selectionModel()->clear();
    //the treeModel here is QTreeModel, refer to snippet below
    d->treeModel()->clear();
}


void QTreeModel::clear()
{
    SkipSorting skipSorting(this);
    for (int i = 0; i < rootItem->childCount(); ++i) {
        QTreeWidgetItem *item = rootItem->children.at(i);
        item->par = 0;
        item->view = 0;
        delete item;
    }
....
}

To you actual question about search implementation. First here is a nice snippet for implementation of the actual search functionality (not real-time). (sorry its c++ but i hope its fine).

To perform a real time search you have to connect a void textChanged(const QString & text) signal from QLineEdit to a slot where the search is performed.

I hope that answers your question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文