关于以前的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文件,但在此之前,我会以there小部件的形式保存它们。我只是将小部件添加到一个集合中,然后从那里调用它们。这在Python级别上工作得很好。现在,我有一个按钮,可以从窗口本身内部创建窗口的新实例。那也行。但只能等到第三次开庭。在那一点上,我失去了Qt水平上对我的
QPushButton
的引用。我明白了wrapped C/C++ object of type `QPushButton` has been deleted
尝试从我的收藏中检索按钮时出错(
equationEditor
)。在Python中,它们仍然存在,但很明显,由于我在某个地方错误地处理了引用,相应的Qt对象被销毁了。有人能指出更好的解决方案吗?或者我怎样才能保留推荐信?
谢谢。。。
编辑:
由于似乎有一些混淆,我将尝试更详细地解释该功能。
程序启动并创建一个窗口“方程式1”,其中有一个
QListView
和一个QPushButton
“1”。列表视图中列出了所有可用的QPushButton
(仅在开始时列出一个项)。在我的实际程序中,QPushButton
是带有一些文本字段和QWidget
的QPushButton
。如果用户单击“1”,则按钮“1”应消失,名为“2”的新
QPushButton
实例应出现在“1”位置。另外,listview现在应该包含两个条目“1”和“2”,窗口的标题应该是“等式2”。如果它是一个新窗口或与以前相同的新内容是不相关的。两种变体都可以。前者是目前实施的方式。一次只能看到一个窗口。所有
QPushButton
的实例都应该收集在一个小列表中(称为equationEditor
),以将它们保存在内存中。在我的实际程序中,这用于保存小部件中所做的所有更改,而不将更改写入临时文件。稍后,如果用户在
QListView
中选择项目“1”,则当前可见的QPushButton
应替换为QPushButton
“1”(来自集合equationEditor
),或者如果用户选择第二个项目,则应显示QPushButton
“2”。为什么?
稍后将使用的小部件包含大量可编辑数据。因为用户可以在任何时候编辑它,所以在不显示窗口小部件的情况下将它们保存在内存中会更容易,而不是再次重新填充所有数据。一旦用户在
QListView
中选择一个,相应的小部件就会显示在窗口中,以便他可以再次编辑小部件中的数据。 最佳答案
很难理解你到底想做什么。看着你的代码,我想知道为什么在失败前它还能工作两次。
顺便说一下,我刚刚看到,在上一篇文章中,scholli给出了
不管怎样,我认为你应该为方程式窗口编一个新的类。然后,main类可以跟踪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/