Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Designer: Display a List of Enum Options in Custom Widget Properties Editor Using Python
Forum Updated to NodeBB v4.3 + New Features

Qt Designer: Display a List of Enum Options in Custom Widget Properties Editor Using Python

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 563 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Khamisi Kibet
    wrote on last edited by
    #1

    I'm working on creating a custom widget for Qt Designer in Python using PyQt or PySide. I want to expose an enum property in the Qt Designer property editor, so that users can select from a list of predefined options rather than typing in arbitrary values.

    In C++, it is possible to expose an enum to Qt Designer using Q_ENUMS and Q_PROPERTY, as described in this Stack Overflow question.

    I want to achieve the same result in Python. Specifically, I want to:

    • Define an enum in Python that can be used as a property in my custom widget.
    • Ensure that this enum is displayed as a dropdown list in the Qt Designer property editor.

    Here's an outline of what I have so far:

    
    from qtpy.QtCore import Qt, QEasingCurve, QPropertyAnimation, QSize, Property, QPoint, QEnum
    from qtpy.QtWidgets import QCheckBox, QApplication, QLabel
    from enum import Enum
    
    class EasingCurveEnum(Enum):
        Option1 = 0
        Option2 = 1
        Option3 = 2
    
    class QCustomCheckBox(QCheckBox):
        def __init__(self, parent=None):
            super().__init__(parent)
            self._easing_curve = EasingCurveEnum.Option1
            # ...
    
        @Property(EasingCurveEnum, designable=True)
        def animationEasingCurve(self):
            return self._easing_curve
        
        @animationEasingCurve.setter
        def animationEasingCurve(self, curve):
            self._easing_curve = curve
    
    

    How can I make the EasingCurveEnum property appear as a list of options in the Qt Designer property editor?

    I've successfully registered my custom widget in Qt Designer, but the enum list property is not appearing.

    Any guidance or examples would be greatly appreciated!

    Thanks in advance!

    If solution found, please share it here Stackoverflow post

    Pl45m4P 1 Reply Last reply
    0
    • K Khamisi Kibet

      I'm working on creating a custom widget for Qt Designer in Python using PyQt or PySide. I want to expose an enum property in the Qt Designer property editor, so that users can select from a list of predefined options rather than typing in arbitrary values.

      In C++, it is possible to expose an enum to Qt Designer using Q_ENUMS and Q_PROPERTY, as described in this Stack Overflow question.

      I want to achieve the same result in Python. Specifically, I want to:

      • Define an enum in Python that can be used as a property in my custom widget.
      • Ensure that this enum is displayed as a dropdown list in the Qt Designer property editor.

      Here's an outline of what I have so far:

      
      from qtpy.QtCore import Qt, QEasingCurve, QPropertyAnimation, QSize, Property, QPoint, QEnum
      from qtpy.QtWidgets import QCheckBox, QApplication, QLabel
      from enum import Enum
      
      class EasingCurveEnum(Enum):
          Option1 = 0
          Option2 = 1
          Option3 = 2
      
      class QCustomCheckBox(QCheckBox):
          def __init__(self, parent=None):
              super().__init__(parent)
              self._easing_curve = EasingCurveEnum.Option1
              # ...
      
          @Property(EasingCurveEnum, designable=True)
          def animationEasingCurve(self):
              return self._easing_curve
          
          @animationEasingCurve.setter
          def animationEasingCurve(self, curve):
              self._easing_curve = curve
      
      

      How can I make the EasingCurveEnum property appear as a list of options in the Qt Designer property editor?

      I've successfully registered my custom widget in Qt Designer, but the enum list property is not appearing.

      Any guidance or examples would be greatly appreciated!

      Thanks in advance!

      If solution found, please share it here Stackoverflow post

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Khamisi-Kibet

      I guess the same way as in C++. You need to register the enum.

      When using Python enums, with @QEnum

      • https://doc.qt.io/qtforpython-6/PySide6/QtCore/QEnum.html

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      K 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @Khamisi-Kibet

        I guess the same way as in C++. You need to register the enum.

        When using Python enums, with @QEnum

        • https://doc.qt.io/qtforpython-6/PySide6/QtCore/QEnum.html
        K Offline
        K Offline
        Khamisi Kibet
        wrote on last edited by Khamisi Kibet
        #3

        @Pl45m4
        Am assuming that you're referring to this:

        @QEnum
        class EasingCurveEnum(Enum):
        ....
        

        I tried it didnt work.

        I also tried this, did not work either:

        
        class EasingCurveEnum(Enum):
            Option1 = 0
            Option2 = 1
            Option3 = 2
        
        class QCustomCheckBox(QCheckBox):
            def __init__(self, parent=None):
                super().__init__(parent)
                
                 #tried.....
                QEnum(EasingCurveEnum)
        
                self._easing_curve = EasingCurveEnum.Option1
                # ...
        
            @Property(EasingCurveEnum, designable=True)
            def animationEasingCurve(self):
                return self._easing_curve
            
            @animationEasingCurve.setter
            def animationEasingCurve(self, curve):
                self._easing_curve = curve
        
        
        
        JonBJ Pl45m4P 2 Replies Last reply
        0
        • K Khamisi Kibet

          @Pl45m4
          Am assuming that you're referring to this:

          @QEnum
          class EasingCurveEnum(Enum):
          ....
          

          I tried it didnt work.

          I also tried this, did not work either:

          
          class EasingCurveEnum(Enum):
              Option1 = 0
              Option2 = 1
              Option3 = 2
          
          class QCustomCheckBox(QCheckBox):
              def __init__(self, parent=None):
                  super().__init__(parent)
                  
                   #tried.....
                  QEnum(EasingCurveEnum)
          
                  self._easing_curve = EasingCurveEnum.Option1
                  # ...
          
              @Property(EasingCurveEnum, designable=True)
              def animationEasingCurve(self):
                  return self._easing_curve
              
              @animationEasingCurve.setter
              def animationEasingCurve(self, curve):
                  self._easing_curve = curve
          
          
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Khamisi-Kibet
          Please read @Pl45m4's link and the sample there:

          The enumerator must be in a QObject derived class to be registered.

          I know no more than that, nor whether this is the right approach. Try moving your class EasingCurveEnum(Enum) inside class QCustomCheckBox(QCheckBox).

          1 Reply Last reply
          1
          • K Khamisi Kibet

            @Pl45m4
            Am assuming that you're referring to this:

            @QEnum
            class EasingCurveEnum(Enum):
            ....
            

            I tried it didnt work.

            I also tried this, did not work either:

            
            class EasingCurveEnum(Enum):
                Option1 = 0
                Option2 = 1
                Option3 = 2
            
            class QCustomCheckBox(QCheckBox):
                def __init__(self, parent=None):
                    super().__init__(parent)
                    
                     #tried.....
                    QEnum(EasingCurveEnum)
            
                    self._easing_curve = EasingCurveEnum.Option1
                    # ...
            
                @Property(EasingCurveEnum, designable=True)
                def animationEasingCurve(self):
                    return self._easing_curve
                
                @animationEasingCurve.setter
                def animationEasingCurve(self, curve):
                    self._easing_curve = curve
            
            
            
            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Khamisi-Kibet

            Your enum needs to be part of a QObject based class... otherwise moc / QtDesigner won't notice it.
            Check the indentation in my linked example. It's part of the QObject class, not a standalone Python enum class.

            Edit:
            What @JonB wrote above :D


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            K 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @Khamisi-Kibet

              Your enum needs to be part of a QObject based class... otherwise moc / QtDesigner won't notice it.
              Check the indentation in my linked example. It's part of the QObject class, not a standalone Python enum class.

              Edit:
              What @JonB wrote above :D

              K Offline
              K Offline
              Khamisi Kibet
              wrote on last edited by Khamisi Kibet
              #6

              @Pl45m4 @JonB
              Thank you all for your feedback. I read these documentations https://doc.qt.io/qtforpython-6/PySide6/QtCore/QEnum.html , https://doc.qt.io/qtforpython-6.5/PySide6/QtCore/QEnum.html , tried different examples but they didn't work.

              I also attempted the example below which didn't work as I expected:

              
              class QCustomCheckBox(QCheckBox):
                  @QEnum
                  class EasingCurveEnum(Enum):
                      Option1 = 0
                      Option2 = 1
                      Option3 = 2  
                      
                  def __init__(self, parent=None):
                      super().__init__(parent)
              
                      self._easing_curve = EasingCurveEnum.Option1
                      # ...
              
                  @Property(EasingCurveEnum, designable=True)
                  def animationEasingCurve(self):
                      return self._easing_curve
                  
                  @animationEasingCurve.setter
                  def animationEasingCurve(self, curve):
                      self._easing_curve = curve
              

              Images showing my custom properties:

              Screenshot 2024-08-29 103702.png

              Screenshot 2024-08-29 103626.png

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved