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

Replaced widget gets deleted after python variable goes out of scope

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.15.4
    • 5.15.1
    • PySide
    • None
    • Linux/X11
    • eda6ebd5219c18fd79b514c89181089cbb78bf9c (pyside/pyside-setup/dev) 64757c6cc2eeece0974f99ac6e75a46e5f6ab693 (pyside/pyside-setup/5.15)

    Description

      The following script shows a layout with a button that should be replaced by a label (with layout.replaceWidget method) when clicked.

      from PySide2.QtWidgets import QVBoxLayout, QLabel, QPushButton, QWidget, QApplication
      
      app = QApplication()
      win = QWidget()
      layout = QVBoxLayout()
      button = QPushButton("Click to replace")
      
      def do_replace():
          global layout, button
          label = QLabel("Replacement")
          layout.replaceWidget(button, label)
          button.deleteLater()
          #label.parentWidget() # Uncomment this line to "fix" the program
      
      button.clicked.connect(do_replace)
      layout.addWidget(button)
      win.setLayout(layout)
      win.show()
      app.exec_()
      

      As is, when the button is clicked, the button disappears but the label doesn't show up. If the comment on line 13 is removed, the program works; which is very strange, because we are only getting a reference to the parent widget of the newly created label, and doing nothing with it.

      I guess that, by calling parentWidget, some lazy operation is being forcefully executed and the label shows up, while with the comment, Python deletes the reference to the label variable, and Qt cleans up the label before it gets shown.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kleint Friedemann Kleint
            ianliu Ian Liu
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes