def createQmlComponent(filename, engine, context, parent=None): # Load the QML UI (this could take a while...) qcomponent = QDeclarativeComponent( engine, QUrl.fromLocalFile(QML(filename)), parent ) qobject = qcomponent.create(context) error = qcomponent.errorString() if len(error) > 0: print('Error while loading QML file: ' + error) return QmlGuiComponent(qobject, qcomponent)
def __init__(self, parentItem, **kargs): global parent parent = self if not QMLItem.__engine__: QMLItem.__engine__ = QDeclarativeEngine() component = QDeclarativeComponent(QMLItem.__engine__) qmlData = 'import Qt 4.7\n%s { }' % self.__className__ component.setData(qmlData, QUrl()) self._proxy = component.create() self._metaObject = self._proxy.metaObject() self._scene = parentItem.scene() self._scene.addItem(self._proxy) t = type(self) tDict = t.__dict__ for key in tDict: # connect signals if key.startswith('on') and (key in self.__signals__): signalName = self.__signals__[key] signal = getattr(self._proxy, signalName) signal.connect(getattr(self, key)) # initialize properties elif self._metaObject.indexOfProperty(key) != -1: setattr(self, key, QMLProperty(self._proxy, key, tDict[key])) else: try: # istanciate types if issubclass(tDict[key], QMLItem): setattr(self, key, tDict[key](self._proxy)) except TypeError: pass parent = None
from person import Person # int main(int argc, char ** argv) if __name__ == '__main__': # QCoreApplication app(argc, argv); app = QCoreApplication(sys.argv) # qmlRegisterType<Person>("People", 1,0, "Person"); qmlRegisterType(Person, 'People', 1, 0, 'Person'); # QDeclarativeEngine engine; engine = QDeclarativeEngine() # QDeclarativeComponent component(&engine, QUrl("qrc:example.qml")); component = QDeclarativeComponent(engine, QUrl("example.qml")) # Person *person = qobject_cast<Person *>(component.create()); person = component.create() # if (person) { if person: # qWarning() << "The person's name is" << person->name(); # qWarning() << "They wear a" << person->shoeSize() << "sized shoe"; qWarning("The person's name is {p.name}".format(p=person)) qWarning("They wear a {p.shoeSize} sized shoe".format(p=person)) # } else { else: # usual approach failed here: needed extract all the errors from a # list, and then cast them to strings. # trying to cast to unicode resulted in an incomplete error message
# int main(int argc, char ** argv) if __name__ == '__main__': # QCoreApplication app(argc, argv); app = QCoreApplication(sys.argv) # qmlRegisterType<Person>("People", 1,0, "Person"); qmlRegisterType(Person, 'People', 1, 0, 'Person'); # qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty"); qmlRegisterType(BirthdayParty, 'People', 1, 0, 'BirthdayParty'); # QDeclarativeEngine engine; engine = QDeclarativeEngine() # QDeclarativeComponent component(&engine, QUrl("qrc:example.qml")); component = QDeclarativeComponent(engine, QUrl("example.qml")) # BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); party = component.create() # if (party && party->host()) { if party and party.host: # qWarning() << party->host()->name() << "is having a birthday!"; # qWarning() << "They are inviting:"; qWarning("{host.name} is having a birthday!".format(host=party.host)) qWarning("They are inviting:") # for (int ii = 0; ii < party->guestCount(); ++ii) # qWarning() << " " << party->guest(ii)->name(); for x in range(party.guestCount()): qWarning("{}".format(party.guest(x))) # } else {