引用之前的 question ,我需要一些帮助来在我的应用程序中保留引用。
首先是我的代码片段。
from PyQt4 import QtGui
import os, os.path
import sys
class mainWindowHandler():
equationEditor = []
_listview = None
_window = None
def __init__(self):
return
def showAddEquation(self):
"""Creates a new instance of the dynamic editor for adding an equation"""
#create a horizontal split layout
window = QtGui.QWidget()
layout = QtGui.QHBoxLayout()
current = len(self.equationEditor) - 1
de = QtGui.QPushButton(str(current))
self.equationEditor.append(de)
de.clicked.connect(self.clicked)
#fill list view with items from equation collection
listview = QtGui.QListWidget()
for equation in self.equationEditor:
item = QtGui.QListWidgetItem()
item.setText(equation.text())
listview.addItem(item)
layout.addWidget(listview)
layout.addWidget(de)
window.setWindowTitle("Equation {}".format(str(current))
window.setLayout(layout)
self._window = window
self._listview = listview
window.show()
return window
def clicked(self):
"""Method for handling the button events in the solver settings\n
signal = the button hit\n"""
return self.showAddEquation()
if __name__ == "__main__":
path = os.path.dirname(os.path.abspath(__file__))
app = QtGui.QApplication(sys.argv)
ewh = mainWindowHandler()
window = ewh.showAddEquation()
sys.exit(app.exec_())
应用程序将(稍后)创建一个允许操作某些设置的窗口 - 在我的代码示例中由 QPushButton
表示。这些设置后来被写入一个 txt 文件,但在那之前我以小部件的形式保存它们。我只是将小部件添加到一个集合中,然后从那里调用它们。这在 Python 级别上运行良好。
现在,我有一个按钮可以从窗口本身内部创建窗口的新实例。那也行。但仅限于第三次。那时我在 Qt 级别上松开了对我的 QPushButton
的引用。我明白了
wrapped C/C++ object of type `QPushButton` has been deleted
尝试从我的集合中检索按钮时出错(equationEditor
)。在 Python 中,它们仍然存在,但显然相应的 Qt 对象已被销毁,因为我在某处错误处理了引用。
有人可以指出更好的解决方案或者我如何保留引用文献吗? 谢谢...
编辑: 由于似乎存在一些混淆,我将尝试更详细地解释该功能。
程序启动并创建一个带有QListView
和QPushButton
“1”的窗口“Equation 1”。在 ListView 中列出了所有可用的 QPushButton
(在开始时只有 1 个项目)。在我的实际程序中,QPushButton
是带有一些文本字段和 QPushButton
的 QWidget
。
如果用户点击“1”,那么按钮“1”应该消失,一个名为“2”的 QPushButton
的新实例应该出现在“1”的位置。此外, ListView 现在应该包含两个项目“1”和“2”,并且窗口的标题应该是“Equation 2”。如果是新窗口还是跟以前一样有新内容是没有关系的。两种变体都可以。前者是目前的实现方式。一次应该只有一个窗口可见。
QPushButton
的所有实例都应收集在一个小列表(称为 equationEditor
)中,以将它们保存在内存中。在我的实际程序中,这用于保存在小部件中所做的所有更改,而不将更改写入临时文件。
稍后,如果用户在 QListView
中选择项目“1”,则当前可见的 QPushButton
应替换为 QPushButton
“1 "(来自 equationEditor
集合)或者如果他选择第二个项目,则 QPushButton
应该显示“2”。
为什么?
后面要用到的widget里面有很多可编辑的数据。由于用户可以随时编辑,因此更容易将小部件保留在内存中而不显示它们,而不是再次重新填充所有数据。一旦用户在 QListView
中选择了一个,相应的小部件就应该显示在窗口中,以便他可以再次编辑小部件中的数据。
最佳答案
很难理解您到底想做什么。查看您的代码,我想知道为什么它在失败之前甚至工作了两次。
顺便说一句。我刚刚看到,有一个 quite accurate description of why it's failing在上一篇文章中,由 Schollii 提供
无论如何,我认为您应该为方程式窗口创建一个新类。然后主类可以跟踪 equationEditor
列表中所有打开的窗口。一旦创建,它还可以将其他打开的窗口的值添加到新窗口。
这是它的样子
from PyQt4 import QtGui
import os, os.path
import sys
class ShowAddEquation(QtGui.QWidget):
"""Creates a new instance of the dynamic editor for adding an equation"""
def __init__(self,parent=None):
super(ShowAddEquation, self).__init__(parent=parent)
#create a horizontal split layout
layout = QtGui.QHBoxLayout()
self.current = 0
self.de = QtGui.QPushButton(str(self.current))
self.listview = QtGui.QListWidget()
layout.addWidget(self.listview)
layout.addWidget(self.de)
self.setWindowTitle("Equation Editor")
self.setLayout(layout)
self.show()
def setCurrent(self, current):
self.current=current
self.de.setText(str(self.current))
class mainWindowHandler():
equationEditor = []
def __init__(self):
return
def clicked(self):
se = ShowAddEquation()
self.equationEditor.append(se)
se.de.clicked.connect(self.clicked)
current = len(self.equationEditor) - 1
se.setCurrent(current)
for equation in self.equationEditor:
item = QtGui.QListWidgetItem()
item.setText(str(equation.current))
se.listview.addItem(item)
if __name__ == "__main__":
path = os.path.dirname(os.path.abspath(__file__))
app = QtGui.QApplication(sys.argv)
ewh = mainWindowHandler()
ewh.clicked()
sys.exit(app.exec_())
关于python - Qt - 在 Python 中保留小部件的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40416607/