【发布时间】:2017-10-24 18:33:09
【问题描述】:
我正在创建一个继承自 QLabel 的自定义小部件,并且我希望在我的小部件上有一个属性来表示在呈现给用户时必须如何格式化数据。
为此,我正在尝试使用Q_ENUMS,但我并没有取得太大的成功。我可以让属性显示在 Designer 中,但保存的 UI 文件将枚举显示为 PyDMLabel::STRING,而不是我期望的 DisplayFormat::STRING。
这是我的小部件代码:
class PyDMLabel(QLabel, PyDMWidget):
class DisplayFormat:
DEFAULT = 0
STRING = 1
DECIMAL = 2
EXPONENTIAL = 3
HEX = 4
BINARY = 5
Q_ENUMS(DisplayFormat)
"""
A QLabel with support for Channels and more from PyDM
Parameters
----------
parent : QWidget
The parent widget for the Label
init_channel : str, optional
The channel to be used by the widget.
"""
def __init__(self, parent=None, init_channel=None):
QLabel.__init__(self, parent)
PyDMWidget.__init__(self, init_channel=init_channel)
self.setTextFormat(Qt.PlainText)
self.setTextInteractionFlags(Qt.NoTextInteraction)
self.setText("PyDMLabel")
self._display_format_type = PyDMLabel.DisplayFormat.DEFAULT
@pyqtProperty(DisplayFormat)
def displayFormat(self):
return self._display_format_type
@displayFormat.setter
def displayFormat(self, new_type):
if self._display_format_type != new_type:
self._display_format_type = new_type
处理Q_ENUMS和PyQt的正确方法是什么?
【问题讨论】:
标签: python enums properties pyqt qt-designer