"RuntimeError: Internal C++ object (...) already deleted" only when running code on .ui file widgets

49 Views Asked by At

I'm using PySide2 inside Houdini.

I load a .UI file made in QtDesigner which contains a checkbox called cb_wipe_network and is accessible after loading the file as self.ui.cb_wipe_network

I then generate more checkbox widgets using code (generate_engine_UI()) and somehow they don't have any issues.

I have identified that this line of code is directly related to or responsible for the error: widget = layout.itemAt(i).widget() in retrieve_settings()

Here's a fragment of my code that I hope is enough:

class MaterialCreator(QtWidgets.QWidget):
    def __init__(self):
        super(MaterialCreator,self).__init__()

        ui_file = hou.expandString('$MYTOOL/main_window.ui')
        self.ui = QtUiTools.QUiLoader().load(ui_file, parentWidget=self)
        self.setParent(hou.ui.mainQtWindow(), QtCore.Qt.Window)

        ...
     
        self.generate_engine_UI()

        #prints: Check .ui file widget: <PySide2.QtWidgets.QCheckBox(0x7fb672ddfb20, name="cb_wipe_network") at 0x7fb6e00a5e00>
        print(f"Check .ui file widget: {self.ui.cb_wipe_network}") 

        #prints: Check generated widget: <PySide2.QtWidgets.QCheckBox(0x7fb64ff535c0, name="cb_use_redshift") at 0x7fb6e0065e40>
        print(f"Check generated widget: {self.ui.cb_use_redshift}") 

        self.retrieve_settings()

        #prints: Check generated widget again: <PySide2.QtWidgets.QCheckBox(0x7fb64ff535c0, name="cb_use_redshift") at 0x7fb6e0065e40>
        print(f"Check generated widget again: {self.ui.cb_use_redshift}") 

        ###### RuntimeError: ###########################################################
        ###### Internal C++ object (PySide2.QtWidgets.QCheckBox) already deleted.#######
        print(f"Check .ui file widget again: {self.ui.cb_wipe_network}") 
        ################################################################################

    def generate_engine_UI(self):
        """
        Dynamically creates UI checkboxes from JSON files.
        """
        for engine in lookup:
            # create engine enabling checkbox
            checkbox_name = lookup[engine]["engine_enable_checkbox"]
            checkbox_instance = QtWidgets.QCheckBox()
            checkbox_instance.setText(engine)
            checkbox_instance.setObjectName(checkbox_name)
            checkbox_instance.setChecked(True)
            setattr(self.ui, checkbox_name, checkbox_instance)
            self.ui.engines_layout.addWidget( getattr(self.ui, checkbox_name, None) )
            
            # create options checkboxes
            if "options_checkboxes" in lookup[engine]:
                for option in lookup[engine]["options_checkboxes"]:
                    checkbox_name = option["ui_name"]
                    checkbox_instance = QtWidgets.QCheckBox()
                    checkbox_instance.setText(option["name"])
                    checkbox_instance.setObjectName(checkbox_name)
                    checkbox_instance.setChecked(True)
                    setattr(self.ui, checkbox_name, checkbox_instance)
                    self.ui.engine_options_layout.addWidget( getattr(self.ui, checkbox_name, None) )

    def retrieve_settings(self):
        layouts = [self.ui.engines_layout, self.ui.engine_options_layout, self.ui.main_options_layout]
        for layout in layouts:
            for i in range(layout.count()):
                ####### THIS LINE BREAKS IT: ########
                widget = layout.itemAt(i).widget()  
                #####################################
                widget_name = widget.objectName()
                if isinstance(widget, QtWidgets.QCheckBox):
                    if self.settings.contains(widget_name):
                        widget.setChecked(self.settings.value(widget_name))

Why is it happening? Why doesn't the error occur with code-generated widgets?

0

There are 0 best solutions below