PyQt4嵌套类-“RuntimeError:底层C/C++对象已被删除" [英] PyQt4 nested classes - "RuntimeError: underlying C/C++ object has been deleted"

查看:102
本文介绍了PyQt4嵌套类-“RuntimeError:底层C/C++对象已被删除"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个很酷的应用程序,但我似乎缺乏一些知识.在互联网上阅读大量信息和示例,但没有帮助:理解底层C/C++对象已被删除";错误

I'm trying to build a cool app, but it seems I lack some knowledge. Read lots of infos and examples in internet, but it doesn't help: Understanding the "underlying C/C++ object has been deleted" error

好的,这是我要做的:

我从我的 main.py 创建了中央小部件,它工作正常,我没有在这里完整地发布它:

I create central widget from my main.py, which works fine and I don't post it here fully:

self.rw = ReportWidget()
self.setCentralWidget(self.rw)

这是我的中心小部件 - report.py:

And here is my central widget - report.py:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtGui, QtCore

class ReportWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ReportWidget, self).__init__(parent)
        self.setup_ui()

    def setup_ui(self):
        report = QtGui.QVBoxLayout(self)
        report.setAlignment(QtCore.Qt.AlignTop)

        head = QtGui.QHBoxLayout()
        add_label = QtGui.QLabel(u"Add")
        head.addWidget(add_label)

        report.addLayout(head)

        area = QtGui.QScrollArea()
        area.setWidgetResizable(True)
        area.setEnabled(True)
        layout = QtGui.QVBoxLayout()
        layout.setAlignment(QtCore.Qt.AlignTop)
        widget = QtGui.QWidget()
        widget.setLayout(layout)
        area.setWidget(widget)
        report.addWidget(area)

        self.s = layout

        # trying to create first line:
        first_line = Line(self)
        self.s.addWidget(first_line)        
        first_line.set_controls(True, False)

        head = QtGui.QHBoxLayout()
        ok = QtGui.QPushButton(u"Calculate")

        head.addWidget(ok)
        report.addLayout(head)

从同一个文件report.py继续:

class Line(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Line, self).__init__(parent)
        self.setup_ui(parent)

    def setup_ui(self, parent):
        add_button = QtGui.QPushButton()
        add_button.setObjectName("add_button")

        self.add_button = add_button
        self.layout = QtGui.QHBoxLayout(line)
        self.layout.addWidget(add_button)

    def set_controls(self, add_button=True, remove_button=True):
        self.add_button.setEnabled(add_button)

因此,运行 main.py 会在我尝试 setEnabled<的最后一段代码中引发 RuntimeError:底层 C/C++ 对象已删除 错误/code> 新按钮的参数,就好像它从未在任何地方创建或绑定过一样.

Thus, running main.py raises RuntimeError: underlying C/C++ object has been deleted error on the last piece of code where I try to setEnabled parameter to new button, as if it was never created or bound anywhere.

看来我有一些设计缺陷.也许在一个文件或其他文件中有不同的类是错误的想法?或者也许我不太控制哪个小部件具有哪个父级以及布局如何工作.

It seems I have some design flaw. Maybe it's wrong idea to have different classes in one file or else? Or maybe I don't quite control which widget has which parent and how layouts work.

感谢您的阅读.祝你有美好的一天!

Thank you for reading. Have a nice day!

推荐答案

感谢所有尝试回答的人!不幸的是没有人说我写了一堆废话!*微笑*

Thanks to everyone who tried to answer! Unfortunately no one said what a bunch of crap I wrote! *smile*

我的 line 已经是一个小部件,我不需要在自己内部创建自己.我所要做的就是在 setup_ui 中创建布局并向其添加小部件.最后看起来像:

My line is already a widget and I don't need to create itself inside itself. All I had to do is to create layout inside setup_ui and add widgets to it. Finally it looks like:

class Line(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Line, self).__init__(parent)
        self.setup_ui(parent)

    def setup_ui(self, parent):
        line = QtGui.QHBoxLayout(self)

        add_button = QtGui.QPushButton()
        add_button.setObjectName("add_button")

        line.addWidget(add_button)

        # to get reference from outside
        self.add_button = add_button

    def set_controls(self, add_button=True, remove_button=True):
        self.add_button.setEnabled(add_button)

特别感谢 nymk阿瓦里斯

这篇关于PyQt4嵌套类-“RuntimeError:底层C/C++对象已被删除"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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