PyQt6 Qml example that uses QQmlListProperty

I’m trying to make sense of the PyQt6 Qml integration example that uses QQmlListProperty here

The Python file I’m currently using: main.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import sys
from PyQt6.QtCore import pyqtProperty, QCoreApplication, QObject, QUrl
from PyQt6.QtQml import qmlRegisterType, QQmlComponent, QQmlEngine, QQmlListProperty
class Person(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._name = ''
self._shoeSize = 0
@pyqtProperty('QString')
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@pyqtProperty(int)
def shoeSize(self):
return self._shoeSize
@shoeSize.setter
def shoeSize(self, shoeSize):
self._shoeSize = shoeSize
class BirthdayParty(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._guests = []
@pyqtProperty(QQmlListProperty)
def guests(self):
return QQmlListProperty(Person, self, self._guests)
app = QCoreApplication(sys.argv)
qmlRegisterType(Person, 'People', 1, 0, 'Person')
qmlRegisterType(BirthdayParty, 'BirthdayParties', 1, 0, 'BirthdayParty')
engine = QQmlEngine()
person = QQmlComponent(engine)
person.loadUrl(QUrl.fromLocalFile('example.qml'))
person_object = person.create()
party = QQmlComponent(engine)
party.loadUrl(QUrl.fromLocalFile('example1.qml'))
party_object = party.create()
if person_object is not None:
print("The person's name is %s." % person_object.name)
print("They wear a size %d shoe." % person_object.shoeSize)
else:
for error in person_object.errors():
print(error.toString())
</code>
<code>import sys from PyQt6.QtCore import pyqtProperty, QCoreApplication, QObject, QUrl from PyQt6.QtQml import qmlRegisterType, QQmlComponent, QQmlEngine, QQmlListProperty class Person(QObject): def __init__(self, parent=None): super().__init__(parent) self._name = '' self._shoeSize = 0 @pyqtProperty('QString') def name(self): return self._name @name.setter def name(self, name): self._name = name @pyqtProperty(int) def shoeSize(self): return self._shoeSize @shoeSize.setter def shoeSize(self, shoeSize): self._shoeSize = shoeSize class BirthdayParty(QObject): def __init__(self, parent=None): super().__init__(parent) self._guests = [] @pyqtProperty(QQmlListProperty) def guests(self): return QQmlListProperty(Person, self, self._guests) app = QCoreApplication(sys.argv) qmlRegisterType(Person, 'People', 1, 0, 'Person') qmlRegisterType(BirthdayParty, 'BirthdayParties', 1, 0, 'BirthdayParty') engine = QQmlEngine() person = QQmlComponent(engine) person.loadUrl(QUrl.fromLocalFile('example.qml')) person_object = person.create() party = QQmlComponent(engine) party.loadUrl(QUrl.fromLocalFile('example1.qml')) party_object = party.create() if person_object is not None: print("The person's name is %s." % person_object.name) print("They wear a size %d shoe." % person_object.shoeSize) else: for error in person_object.errors(): print(error.toString()) </code>
import sys

from PyQt6.QtCore import pyqtProperty, QCoreApplication, QObject, QUrl
from PyQt6.QtQml import qmlRegisterType, QQmlComponent, QQmlEngine, QQmlListProperty


class Person(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._name = ''
        self._shoeSize = 0

    @pyqtProperty('QString')
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name

    @pyqtProperty(int)
    def shoeSize(self):
        return self._shoeSize

    @shoeSize.setter
    def shoeSize(self, shoeSize):
        self._shoeSize = shoeSize


class BirthdayParty(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)
        self._guests = []

    @pyqtProperty(QQmlListProperty)
    def guests(self):
        return QQmlListProperty(Person, self, self._guests)


app = QCoreApplication(sys.argv)

qmlRegisterType(Person, 'People', 1, 0, 'Person')
qmlRegisterType(BirthdayParty, 'BirthdayParties', 1, 0, 'BirthdayParty')

engine = QQmlEngine()

person = QQmlComponent(engine)
person.loadUrl(QUrl.fromLocalFile('example.qml'))
person_object = person.create()

party = QQmlComponent(engine)
party.loadUrl(QUrl.fromLocalFile('example1.qml'))
party_object = party.create()


if person_object is not None:
    print("The person's name is %s." % person_object.name)
    print("They wear a size %d shoe." % person_object.shoeSize)
else:
    for error in person_object.errors():
        print(error.toString())

example.qml that contains Person named Bob

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import People 1.0
Person {
name: "Bob Jones"
shoeSize: 12
}
</code>
<code>import People 1.0 Person { name: "Bob Jones" shoeSize: 12 } </code>
import People 1.0

Person {
    name: "Bob Jones"
    shoeSize: 12
}

example1.qml that contains the BirtdayParty

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import BirthdayParties 1.0
BirthdayParty {
guests:[
Person {
name: "Bob Jones"
shoeSize: 12
},
Person {
name: "Bob Jones 1"
shoeSize: 13
}
]
}
</code>
<code>import BirthdayParties 1.0 BirthdayParty { guests:[ Person { name: "Bob Jones" shoeSize: 12 }, Person { name: "Bob Jones 1" shoeSize: 13 } ] } </code>
import BirthdayParties 1.0

BirthdayParty {
    guests:[
        Person {
            name: "Bob Jones"
            shoeSize: 12
        },
        Person {
            name: "Bob Jones 1"
            shoeSize: 13
        }
    ]
}

The error I’m getting:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>QQmlComponent: Component is not ready
The person's name is Bob Jones.
They wear a size 12 shoe.
</code>
<code>QQmlComponent: Component is not ready The person's name is Bob Jones. They wear a size 12 shoe. </code>
QQmlComponent: Component is not ready
The person's name is Bob Jones.
They wear a size 12 shoe.

According to my research the ‘Component not ready’ indicates an error in Qml syntax. Also if I change example1.qml to

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import BirthdayParties 1.0
BirthdayParty {
guests:[
]
}
</code>
<code>import BirthdayParties 1.0 BirthdayParty { guests:[ ] } </code>
import BirthdayParties 1.0

BirthdayParty {
    guests:[
    ]
}

the script runs without errors. So I guess I don’t know how to make a list of Qml objects in a qml file. This example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import QtQuick
Item {
states: [
State { name: "loading" },
State { name: "running" },
State { name: "stopped" }
]
}
</code>
<code>import QtQuick Item { states: [ State { name: "loading" }, State { name: "running" }, State { name: "stopped" } ] } </code>
import QtQuick

Item {
    states: [
        State { name: "loading" },
        State { name: "running" },
        State { name: "stopped" }
    ]
}

from here is an example of a list of objects but I can’t make my list work.

I am running main.py from the Windows 10 terminal.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật