简体   繁体   English

如何在PyQt中使用Q_ENUMS

[英]How to use Q_ENUMS in PyQt

I am creating a custom widget that inherits from QLabel , and I would like to have a property on my widget to represent how the data must be formatted when presenting to the user. 我正在创建一个继承自QLabel的自定义窗口小部件,我希望在我的窗口小部件上有一个属性来表示在向用户呈现时必须如何格式化数据。

For that I am trying to use Q_ENUMS , but I'm not having much success. 为此,我试图使用Q_ENUMS ,但我没有取得多大成功。 I can get the property to show in Designer, but the UI file saved shows the enum as PyDMLabel::STRING and not as I would expect DisplayFormat::STRING . 我可以在Designer中显示该属性,但保存的UI文件将枚举显示为PyDMLabel::STRING而不是我期望的DisplayFormat::STRING

Here is my code for the widget: 这是我的小部件代码:

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

What is the correct way to deal with Q_ENUMS and PyQt? 处理Q_ENUMS和PyQt的正确方法是什么?

In order for Qt (Designer) to see an enum, PyQt has to add it to the meta-object of the custom class. 为了让Qt(Designer)看到枚举,PyQt必须将它添加到自定义类的元对象中。 So it could never be referred to by Qt as DisplayFormat::STRING . 所以它永远不会被Qt称为DisplayFormat::STRING

In Qt, enums declared in the class scope expose their constants as members of the class. 在Qt中,在类范围中声明的枚举将它们的常量暴露为类的成员。 So for example, the QComboBox class defines an InsertPolicy enum, and the constants can be referred to like this: QComboBox::InsertAtTop . 例如, QComboBox类定义了一个InsertPolicy枚举,常量可以像这样引用: QComboBox::InsertAtTop So in that respect, the behaviour of PyQt Q_ENUMS in Qt Designer plugins is exactly as expected, since the ui file shows PyDMLabel::STRING . 所以在这方面,PyQt Q_ENUMS在Qt Designer插件中的行为完全符合预期,因为ui文件显示了PyDMLabel::STRING

However, getting fully equivalent behaviour in Python code requires some extra work. 但是,在Python代码中获得完全等效的行为需要一些额外的工作。 The nearest I could come up with is this: 我能想出的最近的是:

class DisplayFormat:
    DEFAULT = 0
    STRING = 1
    DECIMAL = 2
    EXPONENTIAL = 3
    HEX = 4
    BINARY = 5

class PyDMLabel(QLabel, PyDMWidget, DisplayFormat):
    DisplayFormat = DisplayFormat

    Q_ENUMS(DisplayFormat)

This will still result in Qt Designer using PyDMLabel::STRING (as expected). 仍将导致Qt Designer使用PyDMLabel::STRING (如预期的那样)。 But Python code can now access the constants in any of these ways: 但Python代码现在可以通过以下任何方式访问常量:

PyDMLabel.STRING
PyDMLabel.DisplayFormat.STRING
DisplayFormat.STRING

And in fact, if you don't mind losing the second of these options, you could simplify things even further to this: 事实上,如果你不介意丢失其中的第二个选项,你可以进一步简化:

class DisplayFormat:
    DEFAULT = 0
    ...    

class PyDMLabel(QLabel, PyDMWidget, DisplayFormat):    
    Q_ENUMS(DisplayFormat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM