Other Support for Dynamic Meta-objects

PyQt5 creates a QMetaObject instance for any Python sub-class of QObject without the need for the equivalent of Qt’s Q_OBJECT macro. Most of a QMetaObject is populated automatically by defining signals, slots and properties as described in previous sections. In this section we cover the ways in which the remaining parts of a QMetaObject are populated.

Q_ENUMS() and Q_FLAGS()

New in version 5.2.

The Q_ENUMS() and Q_FLAGS() functions declare enumerated types and flag types respectively that are published in the QMetaObject. The typical use in PyQt5 is to declare symbolic constants that can be used by QML, and as type of properties that can be set in Qt Designer.

Each function takes a number of Python type objects that implement the enumerated or flag type. For example:

from PyQt5.QtCore import Q_ENUMS, Q_FLAGS, QObject


class Instruction(QObject):

    class Direction:
        Up, Down, Left, Right = range(4)

    Q_ENUMS(Direction)

    class Status:
        Null = 0x00
        Urgent = 0x01
        Acknowledged = 0x02
        Completed = 0x04

    Q_FLAGS(Status)

Q_CLASSINFO()

The Q_CLASSINFO() function is used in the same way as Qt’s macro of the same name, i.e. it is called from a class’s definition in order to specify a name/value pair that is placed in the class’s QMetaObject.

For example it is used by QML to define the default property of a class:

from PyQt5.QtCore import Q_CLASSINFO, QObject


class BirthdayParty(QObject):

    Q_CLASSINFO('DefaultProperty', 'guests')