Pyside createObject() Bug

  • I am porting this javascript tutorial into pyside (for some reason its in pyside doc but not actually written in python):

    And am having difficulty calling the createObject() method of a QDeclarativeComponent object that gets called whenever it is time to make a new block for this "samegame" example.

    Here is my pyside game logic module (normally javascript in the above tutorial):

    @ import sys
    from PySide.QtCore import *
    from PySide.QtGui import *
    from PySide.QtDeclarative import *
    from math import floor

    class MainWindow(QDeclarativeView):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.setWindowTitle("Main Window")
            # Renders 'view.qml'
            # QML resizes to main window
            self.blockSize = 40
            self.maxColumn = 10
            self.maxRow = 15
            self.maxIndex = self.maxColumn * self.maxRow
            self.board = []
            self.component = None
        #Index function used instead of a 2D array
        def index(self, column, row):
            return column + (row * self.maxColumn)
        def startNewGame(self):
            #Delete blocks from previous game
            for b in self.board: 
            bgnd = self.rootObject().findChild(QDeclarativeItem, name="background")
            width = QDeclarativeProperty(bgnd, "width")
            height = QDeclarativeProperty(bgnd, "height")
            #Calculate board size
            self.maxColumn = int(floor( / self.blockSize))
            self.maxRow = int(floor( / self.blockSize))
            self.maxIndex = self.maxColumn * self.maxRow
            #Initialize Board
            self.board = [None for _ in range(self.maxIndex)]
            for column in range(self.maxColumn):
                for row in range(self.maxRow):
                    self.createBlock(column, row)
        def createBlock(self, column, row):
            if self.component is None: 
                engine = QDeclarativeEngine()
                self.component = QDeclarativeComponent(engine, 
            # Note that if Block.qml was not a local file, component.status would be
            # Loading and we should wait for the component's statusChanged() signal to
            # know when the file is downloaded and ready before calling createObject().
            if self.component.isReady():
                bgnd = self.rootObject().findChild(QDeclarativeItem, 
                print bgnd
                dynamicObject = self.component.createObject(bgnd)
                if dynamicObject is None:
                    print "error creating block"
                    return False
                dynamicObject.x = column * self.blockSize
                dynamicObject.y = row * self.blockSize
                dynamicObject.width = self.blockSize
                dynamicObject.height = self.blockSize
                self.board[self.index(column, row)] = dynamicObject
                print "error loading block component"
                return False
            return True
    if __name__ == '__main__':
        # Create the Qt Application
        app = QApplication(sys.argv)
        # Create and show the main window
        window = MainWindow()
        context = window.rootContext()
        # Run the main Qt loop

    Note: All necessary QML files use QtQuick 1.0 and are identical to the tutorial so I dont repeat them here.

    When I run this code, it crashes at:

    @ dynamicObject = self.component.createObject(bgnd)@


    @ TypeError: Unknown type used to call meta function (that may be a signal): QScriptValue@

    any ideas how I'm misusing this createObject method? I understand the parent must be a QObject and that it is not optional. Otherwise, I'm not entirely able to understand from the docs what more it should constitute:


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.