Qt Designer: Display a List of Enum Options in Custom Widget Properties Editor Using Python
-
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
-
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
I guess the same way as in C++. You need to register the enum.
When using Python enums, with
@QEnum
-
I guess the same way as in C++. You need to register the enum.
When using Python enums, with
@QEnum
@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
-
@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
@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)
insideclass QCustomCheckBox(QCheckBox)
. -
@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
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 theQObject
class, not a standalone Python enum class.Edit:
What @JonB wrote above :D -
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 theQObject
class, not a standalone Python enum class.Edit:
What @JonB wrote above :D@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: