Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1893

C++ object deleted in slot when UiTools + nativeParentWidget used

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.15.15
    • 6.2.3
    • PySide
    • None
    • Windows Version: 10.0.19042 Build 19042
      PySide6 version: 6.2.3
      Python version: 3.8.7
    • Windows
    • e5d50b1e9f (pyside/pyside-setup/dev) 216a5c1054 (pyside/pyside-setup/6.3) 216a5c1054 (pyside/tqtc-pyside-setup/6.3) e5d50b1e9f (pyside/tqtc-pyside-setup/dev) 8ffb90ebee (pyside/tqtc-pyside-setup/5.15) 261e65ba52 (pyside/tqtc-pyside-setup/6.2)

    Description

      Run the following code, clicking the button three times:

      from PySide6 import QtWidgets, QtUiTools
      
      def make2():
          x = QtWidgets.QPushButton("Switch back", w)
          x.clicked.connect(lambda: w.setCentralWidget(make1()))
          x.nativeParentWidget()  # No error if this line is commented out
          return x
      
      def make1():
          loader = QtUiTools.QUiLoader()
          inner = loader.load("dbg.ui", w)
          switchButton = inner.findChild(QtWidgets.QPushButton, "switchButton")
          switchButton.clicked.connect(
              lambda: print(switchButton) or w.setCentralWidget(make2())
          )
          return inner
      
      app = QtWidgets.QApplication()
      w = QtWidgets.QMainWindow()
      w.setCentralWidget(make1())
      w.show()
      app.exec_()
      

      The output for me is:

      qt.pysideplugin: Environment variable PYSIDE_DESIGNER_PLUGINS is not set, bailing out.
      qt.pysideplugin: No instance of QPyDesignerCustomWidgetCollection was found.
      Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute and QSGRendererInterface::OpenGLRhi using QQuickWindow::setGraphicsApi before constructing QGuiApplication.
      c:/Users/matthew.joyce/Repos/lw8/dbg_deleted_qobjects.py:25: DeprecationWarning: 'exec_' will be removed in the future. Use 'exec' instead.
        app.exec_()
      <PySide6.QtWidgets.QPushButton(0x25e7e7b41d0, name="switchButton") at 0x0000025E7FA20640>
      qt.pysideplugin: No instance of QPyDesignerCustomWidgetCollection was found.
      Traceback (most recent call last):
        File "c:/Users/matthew.joyce/Repos/lw8/dbg_deleted_qobjects.py", line 16, in <lambda>
          lambda: print(switchButton) or w.setCentralWidget(make2())
      RuntimeError: Internal C++ object (PySide6.QtWidgets.QPushButton) already deleted.
      

      Commenting out the line calling nativeParentWidget fixes the problem, as does not using the QUiLoader. UI file is (to be saved as "dbg.ui"):

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>Widget</class>
       <widget class="QWidget" name="Widget">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>93</width>
          <height>41</height>
         </rect>
        </property>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <widget class="QPushButton" name="switchButton">
           <property name="text">
            <string>Switch</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      Attachments

        1. pyside1893.zip
          3 kB
          Friedemann Kleint
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kleint Friedemann Kleint
            matsjoyce-refeyn Matthew Joyce
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: