2014-02-19 72 views
1

大廈QML項目關閉此Pyside教程: 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如何動態實例在Pyside

我試圖盡一切Python和沒有任何Java腳本。

我呼籲這是很好的描述爲「動態對象管理」這裏QDeclarativeComponent的的CreateObject()方法時,遇到的唯一困難: http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.html

因此,這裏是一個光禿禿的骨頭的例子,導致錯誤:

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("Block2.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_()) 

隨着主窗口QML文件內容( 「game2.qml」):

import QtQuick 1.0 

Rectangle { 
    id: screen 

    width: 490; height: 720 

    SystemPalette { id: activePalette } 
} 

和QML對象,我想動態構造( 「Block2.qml」):

import QtQuick 1.0 

Rectangle { 
    id: block 
} 

當我運行這段代碼,它崩潰的:

dynamicObject = self.component.createObject(self.rootObject()) 

有:

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

我知道父母必須是QObject,但除此之外,我不完全確定文檔中應該包含多少內容: http://srinikom.github.io/pyside-docs/PySide/QtDeclarative/QDeclarativeComponent.html

這是不是在C++按照問題: https://qt-project.org/forums/viewthread/7717

它清楚地只是一個問題Pyside目前。

任何想法可能會導致此問題?潛在的錯誤?

回答

0

解決方法是依賴於javascript來創建對象,而其他所有東西都是python。在這個實現中,你將組件及其父項的qml文件傳遞給創建該組件的javascript實現。它做了對象的基本構造。儘管爲了簡潔起見,將會是理想的純Python解決方案。

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 
    parent = self.rootObject() 
    view = QDeclarativeView() 
    view.setSource(QUrl.fromLocalFile("comp_create.qml")) 
    block = view.rootObject().create("Block2.qml", parent) 
    print block 
    block.x = 100 
    block.y = 200 
    # prove that we created the object 
    print block.x, block.y 

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

唯一加入QML是使用JavaScript,因爲Pyside目前不會工作,以創建對象的組件創建者( 「comp_create.qml」):

import QtQuick 1.0 

Item { 
    id: creator 

    function create(qml_fname, parent) { 
     // for a given qml file and parent, create component 
     var comp = Qt.createComponent(qml_fname); 
     if (comp.status == Component.Ready) { 
      // create the object with given parent 
      var ob = comp.createObject(parent); 
      if (ob == null) { 
       // Error Handling 
       console.log("Error creating object"); 
      } 
      return ob 
     } else if (component.status == Component.Error) { 
      // Error Handling 
      console.log("Error loading component:", component.errorString()); 
     } 
     else { 
      component.statusChanged.connect(finishCreation); 
     } 
     return null 
    } 
} 

注意,從借用了這個代碼主要是: http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.html