扩展 QML - 对象和列表属性类型示例#
导出 C++ 属性。
此示例基于 扩展 QML - 添加类型示例。
对象和列表属性类型示例显示了如何在 QML 中添加对象和列表属性。此示例添加了一个 BirthdayParty 类型,用于指定生日派对,包括庆祝者和宾客列表。人们使用在之前示例中构建的 People QML 类型进行指定。
import examples.properties.people
BirthdayParty {
host: Person {
name: "Bob Jones"
shoe_size: 12
}
guests: [
Person { name: "Leo Hodges" },
Person { name: "Jack Smith" },
Person { name: "Anne Brown" }
]
}
声明 BirthdayParty#
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中,列表属性的类型,包括嘉宾属性(其类型为人物列表),都是ListProperty类型。ListProperty是一种简单的数据类型,包含了若干函数。当QML需要从列表中读取、写入或与之交互时,会调用这些函数。除了具体的列表,如示例中使用的“人物列表”外,QQmlListProperty的使用还允许实现“虚拟列表”和其他高级场景。
运行示例#
示例中的main.py文件包含一个简单的Shell应用程序,用于加载并运行本页开头所显示的QML片段。
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
"""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 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 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