扩展QML - 对象和列表属性类型示例¶
导出C++属性。
这个例子基于 扩展 QML - 添加类型示例。
对象和列表属性类型示例展示了如何在QML中添加对象和列表属性。此示例添加了一个BirthdayParty类型,用于指定一个生日派对,包括一个庆祝者和一个客人列表。人员使用上一个示例中内置的People QML类型来指定。
导入示例属性.人员
BirthdayParty {
host: Person {
name: "Bob Jones"
shoe_size: 12
}
guests: [
Person { name: "Leo Hodges" },
Person { name: "Jack Smith" },
Person { name: "Anne Brown" }
]
}
声明生日派对¶
BirthdayParty 类的声明如下:
from person import Person
# To be used on the @QmlElement decorator
# (QML_IMPORT_MINOR_VERSION is optional)
QML_IMPORT_NAME = "People"
QML_IMPORT_MAJOR_VERSION = 1
@QmlElement
class BirthdayParty(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._host = None
self._guests = []
@Property(Person)
def host(self):
return self._host
@host.setter
def host(self, h):
self._host = h
def guest(self, n):
return self._guests[n]
def guestCount(self):
return len(self._guests)
def appendGuest(self, guest):
self._guests.append(guest)
guests = ListProperty(Person, appendGuest)
该类包含一个用于存储庆祝者对象的成员,以及一个用于存储Person实例的列表成员。
在QML中,列表属性的类型 - 以及guests属性是一个人员列表 - 都是ListProperty
类型。
ListProperty
是一个简单的值类型,包含一组函数。
每当QML需要读取、写入或以其他方式与列表交互时,它都会调用这些函数。除了像本示例中使用的人员列表这样的具体列表外,使用ListProperty
还允许“虚拟列表”和其他高级场景。
运行示例¶
示例中的 main.py 文件包含一个简单的 shell 应用程序,它加载并运行本页开头显示的 QML 片段。
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations
"""PySide6 port of the qml/examples/qml/referenceexamples/properties example from Qt v6.x"""
from pathlib import Path
import sys
from PySide6.QtCore import QCoreApplication
from PySide6.QtQml import QQmlComponent, QQmlEngine
from person import Person # noqa: F401
from birthdayparty import BirthdayParty # noqa: F401
if __name__ == '__main__':
app = QCoreApplication(sys.argv)
engine = QQmlEngine()
engine.addImportPath(Path(__file__).parent)
component = QQmlComponent(engine)
component.loadFromModule("People", "Main")
party = component.create()
if party:
print(f"{party.host} is having a birthday!\nThey are inviting:")
for g in range(party.guestCount()):
name = party.guest(g).name
print(f" {name}")
else:
print(component.errors())
del engine
sys.exit(0)
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations
from PySide6.QtCore import QObject, Property
from PySide6.QtQml import QmlElement
# To be used on the @QmlElement decorator
# (QML_IMPORT_MINOR_VERSION is optional)
QML_IMPORT_NAME = "People"
QML_IMPORT_MAJOR_VERSION = 1
@QmlElement
class Person(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._name = ''
self._shoe_size = 0
@Property(str)
def name(self):
return self._name
@name.setter
def name(self, n):
self._name = n
@Property(int)
def shoe_size(self):
return self._shoe_size
@shoe_size.setter
def shoe_size(self, s):
self._shoe_size = s
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations
from PySide6.QtCore import QObject, Property
from PySide6.QtQml import QmlElement, ListProperty
from person import Person
# To be used on the @QmlElement decorator
# (QML_IMPORT_MINOR_VERSION is optional)
QML_IMPORT_NAME = "People"
QML_IMPORT_MAJOR_VERSION = 1
@QmlElement
class BirthdayParty(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._host = None
self._guests = []
@Property(Person)
def host(self):
return self._host
@host.setter
def host(self, h):
self._host = h
def guest(self, n):
return self._guests[n]
def guestCount(self):
return len(self._guests)
def appendGuest(self, guest):
self._guests.append(guest)
guests = ListProperty(Person, appendGuest)
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import People
BirthdayParty {
host: Person {
name: "Bob Jones"
shoe_size: 12
}
guests: [
Person { name: "Leo Hodges" },
Person { name: "Jack Smith" },
Person { name: "Anne Brown" }
]
}
module People
typeinfo coercion.qmltypes
Main 1.0 Main.qml