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

setProperty() does not work with Enums

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 6.8.2, 6.8.3
    • PySide
    • None
    • Windows 11 (though almost certainly cross-platform)
    • Windows

    Description

      Bug Description

      When setting a property that is an Enum type using the QObject.setProperty() method, the setProperty() method will fail unless an integer is passed into the function. This primarily causes issues with pyside6-uic generated code for custom widgets.

      Example

      Seen in the simple example below, setting the property using the (correct) enumeration fails and does not change anything unless casting to int first.

      from PySide6.QtWidgets import QWidget
      
      widget = QWidget()
      
      result = widget.setProperty("focusPolicy", Qt.FocusPolicy.ClickFocus)
      print(widget.focusPolicy())  # Prints '0' (Representing Qt.FocusPolicy.NoFocus)
      print(result)  # Prints 'False' as it failed to set the property
      
      result = widget.setProperty("focusPolicy", int(Qt.FocusPolicy.ClickFocus)) 
      print(widget.focusPolicy()) # Prints '2' (Representing Qt.FocusPolicy.ClickFocus)
      print(result) # Prints 'True' as it successfully set the property
      

      This occurs for seemingly every Qt enumeration. While wrapping the enumeration in an int is a hacky but easily workable solution for hand-written code, this primarily causes issues for pyside6-uic as it uses the setProperty method to set properties for custom widgets and does not wrap the Enumerations in ints. This then causes issues as the compiled UI file will not correctly set properties.

      Current workarounds

      Until this is resolved, the most obvious workarounds are to either post-process the code generated by pyside6-uic to wrap Enums with an integer value, or to set these values after creating the widget with the former being more reliable.

      Potential solutions

      A simple solution that would solve the immediate issue of the pyside6-uic generating the incorrect code is for pyside6-uic to wrap all generated enums with an int and could act as a quick-stop solution that would solve the immediate problems.

      However, a more robust solution would be to fix the setProperty() method directly so that it can manage enumerations.

      (Potentially) Related Links

      Attachments

        Issue Links

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

          Activity

            People

              crmaurei Cristian Maureira-Fredes
              ciarangruber Ciaran Gruber
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes