Qt World Summit: Submit your Presentation

Pyside Dynamic Object Management of QML

  • Building off this Pyside tutorial:

    I 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:

    So 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
      # QML resizes to main window
      # 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()
     # Run the main Qt loop

    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())@


    @ 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:

    Any idea what might be causing this issue? Potential bug?

Log in to reply