@QmlAttached#

此装饰器声明包含的类型将作为附加属性附加到其他类型。如果类型通过 QmlElement()@QmlNamedElement() 装饰器暴露给 QML,则该声明生效。

QML_IMPORT_NAME = "com.library.name"
QML_IMPORT_MAJOR_VERSION = 1
QML_IMPORT_MINOR_VERSION = 0 # Optional

@QmlAnonymous
class LayoutAttached(QObject):
    @Property(QMargins)
    def margins(self):
       ...

@QmlElement()
@QmlAttached(LayoutAttached)
class Layout(QObject):
      ...

之后,此类可以用在 QML 中

import com.library.name 1.0

Layout {
   Widget {
       Layout.margins: [2, 2, 2, 2]
   }
}