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

发布于 2024-10-24 04:14:26 字数 890 浏览 1 评论 0原文

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

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

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

感谢您抽出时间。

更新(代码)

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()

那么我是否以错误的方式处理此问题?

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

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.

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.

Thanks for your time.

Update (Code)

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()

So am I going about this the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋顶上的小猫咪 2024-10-31 04:14:26

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

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

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

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:

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

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

强辩 2024-10-31 04:14:26

请参阅此处:PySide 陷阱

如果 QObject 超出范围
Python,它将被删除。你有
照顾保持参考
对象:

  • 将其存储为您保留的对象的属性,例如
    self.window = QMainWindow()
  • 将父 QObject 传递给对象的构造函数,因此它被拥有
    由家长提供

See here: 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文