Pyside Dynamic Object Management of QML
-
Building off this Pyside tutorial:
http://qt-project.org/wiki/PySide_QML_Tutorial_Advanced_1
http://qt-project.org/wiki/PySide_QML_Tutorial_Advanced_2
http://qt-project.org/wiki/PySide_QML_Tutorial_Advanced_3
http://qt-project.org/wiki/PySide_QML_Tutorial_Advanced_4I am attempting to do everything in Python and not have any javascript.
The only difficulty I've run into is when calling the createObject() method of a QDeclarativeComponent which is described nicely as a "Dynamic Object Management" here:
http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.htmlSo here is a bare bones example that causes the error:
@ import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *class MainWindow(QDeclarativeView): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setWindowTitle("Main Window") # Renders game screen self.setSource(QUrl.fromLocalFile('game2.qml')) # QML resizes to main window self.setResizeMode(QDeclarativeView.SizeRootObjectToView) # a qml object I'd like to add dynamically self.component = QDeclarativeComponent(QDeclarativeEngine(), QUrl.fromLocalFile("Block.qml")) # check if were ready to construct the object if self.component.isReady(): # create the qml object dynamically dynamicObject = self.component.createObject(self.rootObject()) if __name__ == '__main__': # Create the Qt Application app = QApplication(sys.argv) # Create and show the main window window = MainWindow() window.show() # Run the main Qt loop sys.exit(app.exec_())@
With main window QML file contents ("game2.qml"):
@ import QtQuick 1.0
Rectangle { id: screen width: 490; height: 720 SystemPalette { id: activePalette } }@
And QML object I'd like to dynamically construct ("Block2.qml"):
@ import QtQuick 1.0
Rectangle { id: block }@
When I run this code, it crashes at:
@ dynamicObject = self.component.createObject(self.rootObject())@
with:
@ TypeError: Unknown type used to call meta function (that may be a signal): QScriptValue@
I understand the parent must be a QObject but otherwise I'm not entirely sure from the docs what more it should constitute:
http://srinikom.github.io/pyside-docs/PySide/QtDeclarative/QDeclarativeComponent.htmlAny idea what might be causing this issue? Potential bug?