Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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_4

    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:
    http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.html

    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
      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.html

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


Log in to reply