gpt4 book ai didi

garbage-collection - PyQt:RuntimeError:包装的C/C++对象已被删除

转载 作者:行者123 更新时间:2023-12-03 20:39:15 25 4
gpt4 key购买 nike

如果我运行此代码:

    #!/usr/local/bin/    python3

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class Window(QMainWindow):

def __init__(self):
super().__init__()
self.button1 = QPushButton("1")
self.button2 = QPushButton("2")
self.setCentralWidget(self.button1)
self.button1.clicked.connect(lambda: self.setCentralWidget(self.button2))
self.button2.clicked.connect(lambda: self.setCentralWidget(self.button1))
self.show()

if __name__ == '__main__':

import sys
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())

...我得到以下输出:
Traceback (most recent call last):
File "test.py", line 16, in <lambda>
self.button2.clicked.connect(lambda: self.setCentralWidget(self.button1))
RuntimeError: wrapped C/C++ object of type QPushButton has been deleted

我不明白为什么要删除该对象。窗口应保留对此的引用。
我已经彻底调查了这些帖子:
Understanding the “underlying C/C++ object has been deleted” error
Can a PyQt4 QObject be queried to determine if the underlying C++ instance has been destroyed?

为什么删除按钮?

最佳答案

这个问题的答案如下:
Python PySide (Internal c++ Object Already Deleted)

显然,使用setCentralWidget将一个小部件分配给QMainWindow,然后使用setCentralWidget分配另一个小部件将导致基础c++ QWidget被删除,即使我有一个对象保持对其的引用。

Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.

关于garbage-collection - PyQt:RuntimeError:包装的C/C++对象已被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com