Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Pyside Dynamic Object Management of QML
Forum Update on Monday, May 27th 2025

Pyside Dynamic Object Management of QML

Scheduled Pinned Locked Moved Language Bindings
1 Posts 1 Posters 879 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bijdeb
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved