PyQT 崩溃(底层 C/C++ 对象已被删除)“之后”清除 QTreeWidget
我尝试为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了基于 Neox 的第二个想法的解决方法。
我迭代树中的所有项目并将隐藏标志设置为 true ( setHidden(True) )
然后我迭代foundItems并将隐藏标志设置为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 )
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.
每当您在
QTreeWidgetItem
上调用clear
函数时,项目都会被删除,因此您得到的错误并不奇怪。您可以在以下QTreeWidget
实现摘录中看到它:给您关于搜索实现的实际问题。首先这里是一个很好的代码片段,用于实现实际的搜索功能(不是真实的)时间)。 (抱歉,它是 C++,但我希望它没问题)。
要执行实时搜索,您必须将
void textChanged(const QString & text)
信号从QLineEdit
连接到执行搜索的槽。我希望能回答你的问题
Whenever you call
clear
function on aQTreeWidgetItem
the items are deleted so the error you get is no surprise. You can see it in the following excerpt of theQTreeWidget
implementation: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 fromQLineEdit
to a slot where the search is performed.I hope that answers your question