Python PySide(内部 c++ 对象已删除) [英] Python PySide (Internal c++ Object Already Deleted)

查看:85
本文介绍了Python PySide(内部 c++ 对象已删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently decided to write my first app with Python and PySide. But I have a problem and hope you guys can help.

Python 不断抛出内部 C++ 对象"被删除的异常.根据我对 Python 的有限经验,我认为我的对象超出范围并被 Python 的垃圾收集器删除.

Python keeps raising exceptions that the "Internal C++ Object" is deleted. From my limited experience with Python I figure that my object is going out of scope and being deleted by Python's Garbage Collector.

那么我将如何使用 PySide 在 Python 中设计多页应用程序.并且能够保留我的 QWidgets 以便我可以再次显示页面.

So how would I go about designing a multi-page application in Python with PySide. And being able to keep my QWidgets so I can show the page again.

感谢您的时间.

更新(代码)

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

然后我通常会通过调用来访问主面板:

I would then normally access the mainpanel by calling:

import kernel
kernel.instance().main_panel()

我是不是用错了方法?

推荐答案

经过一番搜索和拉扯,我找到了解决方案.我通过将它们设置为中央小部件来显示所有页面,并在阅读 QMainWindow 文档时 我发现我的小部件基本上被 qt 删除了:

After some searching and hair pulling, I found the solution. I was showing all the pages by setting them as the central widget, and when reading the QMainWindow documentation I found that my widget basically gets deleted by qt as stated:

注意:QMainWindow 拥有小部件指针并将其删除合适的时间.

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

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

So to develop a Multi-Page application rather take a look at QStackedWidget.

这篇关于Python PySide(内部 c++ 对象已删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆