Python PySide(内部c++对象已删除)

2024-06-23 19:33:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近决定用Python和PySide编写我的第一个应用程序。但我有个问题希望你们能帮忙。

Python不断地增加删除“内部C++对象”的异常。根据我对Python的有限经验,我认为我的对象超出了范围,被Python的垃圾收集器删除了。

那么我将如何使用Python和PySide设计一个多页应用程序呢。能够保留我的QWidgets以便我可以再次显示页面。

谢谢你的时间。

更新(代码)

instancing = None
def instance():
   global instancing
   if instancing == None:
      instancing = WPZKernel()
   return instancing

class WPZKernel:
    win = None
    mainscreen = None

    def mainwindow(self):
        if self.win == None:
          self.win = GMKMainWindow(self)
        return self.win

    def main_panel(self):
        if self.mainscreen == None:
           self.mainscreen = GMKMainScreen(self.mainwindow())
        return self.mainscreen

然后,我通常通过调用以下命令访问主面板:

import kernel
kernel.instance().main_panel()

我是不是走错了路?


Tags: 对象instanceselfnone应用程序returnifmain
2条回答

经过一番寻找和头发拉扯,我找到了解决办法。我将所有页面设置为中心小部件来显示它们,当阅读QMainWindow documentation时,我发现我的小部件基本上被qt删除,如前所述:

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

因此,要开发多页应用程序,请查看QStackedWidget

请看这里:PySide Pitfalls

If a QObject falls out of scope in Python, it will get deleted. You have to take care of keeping a reference to the object:

  • Store it as an attribute of an object you keep around, e.g. self.window = QMainWindow()
  • Pass a parent QObject to the object’s constructor, so it gets owned by the parent

相关问题 更多 >

    热门问题