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

__slots__ on Qt derived classes do not work or crash the application

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: P4: Low P4: Low
    • None
    • 6.4.1
    • PySide, Shiboken
    • None
    • Windows; Python 3.11.1; PySide 6.4.1
    • Windows

      First issue

      Consider the following minimal working example:

      import sys
      from PySide6.QtWidgets import QWidget, QMainWindow, QApplication
      
      class SomeClass:
      
          __slots__ = ('_a',)
      
          def __init__(self) -> None:
              self._a: int = 0
              self._b: int = 0 # AttributeError
      
      class MainWindow(QMainWindow):
      
          __slots__ = ('_a',)
      
          def __init__(self, parent: QWidget | None = None) -> None:
              super().__init__(parent)
              self._a: int = 0
              self._b: int = 0 # where is AttributeError?
      
      if __name__ == '__main__':
          # test = SomeClass() # AttributeError
          app = QApplication(sys.argv)
          mainWindow = MainWindow()
          mainWindow.show()
          sys.exit(app.exec())
      
      

      As an example, if `SomeClass` is instantiated, an `AttributeError` will be raised since `b` is not defined in `slots`. The issue is that if `slots` are defined inside a class that derives a Qt class (for example, the `MainWindow` class shown above), an `AttributeError` is no longer raised. It seems as tho defining `slots` does nothing.

      Second issue

      Consider the following minimal working example:

      import sys
      from PySide6.QtWidgets import QWidget, QMainWindow, QApplication
      
      class MainWindowShell(QMainWindow):
      
          __slots__ = ('_a',) # results in a crash
      
          def __init__(self, parent: QWidget | None = None) -> None:
              super().__init__(parent)
              self._a: int = 0
      
      class MainWindow(MainWindowShell):
      
          __slots__ = () # this does nothing
      
          def __init__(self, parent: QWidget | None = None) -> None:
              super().__init__(parent)
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          mainWindow = MainWindow()
          mainWindow.show()
          sys.exit(app.exec())
      
      

      In this example the application will crash (or not launch). Defining `slots` inside a class that derives a Qt class will crash the application if that class serves as a base class.

       

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

            ctismer Christian Tismer
            carlos_s Carlos Souto
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes