1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited. 7## Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). 8## All rights reserved. 9## 10## This file is part of the examples of PyQt. 11## 12## $QT_BEGIN_LICENSE:BSD$ 13## You may use this file under the terms of the BSD license as follows: 14## 15## "Redistribution and use in source and binary forms, with or without 16## modification, are permitted provided that the following conditions are 17## met: 18## * Redistributions of source code must retain the above copyright 19## notice, this list of conditions and the following disclaimer. 20## * Redistributions in binary form must reproduce the above copyright 21## notice, this list of conditions and the following disclaimer in 22## the documentation and/or other materials provided with the 23## distribution. 24## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 25## the names of its contributors may be used to endorse or promote 26## products derived from this software without specific prior written 27## permission. 28## 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 40## $QT_END_LICENSE$ 41## 42############################################################################# 43 44 45import sys 46 47from PyQt5.QtCore import (pyqtProperty, pyqtSlot, QCoreApplication, QObject, 48 QUrl) 49from PyQt5.QtQml import (qmlRegisterType, QQmlComponent, QQmlEngine, 50 QQmlListProperty) 51 52 53QML = b''' 54import QtQuick 2.0 55import People 1.0 56 57BirthdayParty { 58 host: Person { 59 name: "Bob Jones" 60 shoeSize: 12 61 } 62 63 guests: [ 64 Person { name: "Leo Hodges" }, 65 Person { name: "Jack Smith" }, 66 Person { name: "Anne Brown" } 67 ] 68 69 Component.onCompleted: invite("William Green") 70} 71''' 72 73 74class Person(QObject): 75 def __init__(self, parent=None): 76 super(Person, self).__init__(parent) 77 78 self._name = '' 79 self._shoeSize = 0 80 81 @pyqtProperty(str) 82 def name(self): 83 return self._name 84 85 @name.setter 86 def name(self, name): 87 self._name = name 88 89 @pyqtProperty(int) 90 def shoeSize(self): 91 return self._shoeSize 92 93 @shoeSize.setter 94 def shoeSize(self, shoeSize): 95 self._shoeSize = shoeSize 96 97 98class BirthdayParty(QObject): 99 def __init__(self, parent=None): 100 super(BirthdayParty, self).__init__(parent) 101 102 self._host = None 103 self._guests = [] 104 105 @pyqtProperty(Person) 106 def host(self): 107 return self._host 108 109 @host.setter 110 def host(self, host): 111 self._host = host 112 113 @pyqtProperty(QQmlListProperty) 114 def guests(self): 115 return QQmlListProperty(Person, self, self._guests) 116 117 @pyqtSlot(str) 118 def invite(self, name): 119 person = Person(self) 120 person.name = name 121 self._guests.append(person) 122 123 124app = QCoreApplication(sys.argv) 125 126qmlRegisterType(BirthdayParty, "People", 1, 0, "BirthdayParty") 127qmlRegisterType(Person, "People", 1, 0, "Person") 128 129engine = QQmlEngine() 130 131component = QQmlComponent(engine) 132component.setData(QML, QUrl()) 133 134party = component.create() 135 136if party is not None and party.host is not None: 137 print("\"%s\" is having a birthday!" % party.host.name) 138 print("They are inviting:") 139 140 for guest in party.guests: 141 print(" \"%s\"" % guest.name) 142else: 143 for e in component.errors(): 144 print("Error:", e.toString()); 145