2017-03-15 58 views
1

得到根對象我試圖顯示使用的PyQt和QML動態對象,但我得到這個錯誤:PyQt的:如何從QQmlApplicationEngine

win = engine.rootObjects()[0] 

IndexError: list index out of range

這裏是我的代碼的PyQt:

import sys 
import os 
from PyQt5.QtWidgets import QApplication 
from PyQt5.QtQml import QQmlApplicationEngine,QQmlEngine, QQmlComponent 
from PyQt5.QtCore import QObject, pyqtSlot, QVariant,QUrl 


if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    engine = QQmlApplicationEngine() 
    engine.load('main.qml') 
    win = QObject() 
    win = engine.rootObjects()[0] 
    win.show() 
    sys.exit(app.exec_()) 

(PS:與非動態對象,我的代碼運行沒關係)

這是我的main.qml

import QtQuick 2.7 
import QtQuick.Controls 2.0 
import QtQuick.Layouts 1.0 
ApplicationWindow { 
id : root 
visible: true 
width: 1000 
height: 800 
title: qsTr("ACTEMIUM") 
SwipeView { 
    id: swipeView 
    anchors.fill: parent 
    currentIndex: tabBar.currentIndex 
    Page1 { 
     Label { 
      text: qsTr("Page1") 
      anchors.centerIn: parent 
     } 
    } 


    Page2 { 
     Label { 
      text: qsTr("Second page") 
      anchors.centerIn: parent 
     } 
    } 

    Page3 { 
     Label { 
      text: qsTr("Troisieme page") 
      anchors.centerIn: parent 
     } 
    } 
} 

footer: TabBar { 
    id: tabBar 
    currentIndex: swipeView.currentIndex 

    TabButton { 
     text: qsTr("First") 
    } 
    TabButton { 
     text: qsTr("Second") 
    } 
    TabButton { 
     text: qsTr("Trois") 
    } 
} 

}

和我Page1.qml 進口QtQuick 2.7

Page1Form { 
id: root 
button.onClicked: { 
    console.log("OK. Entered text: " + textField.text); 

    var component = Qt.createComponent("main2.qml") 
    if(component.status != Component.Ready) 
    { 
     if(component.status == Component.Error) 
      console.debug("Error:"+ component.errorString()); 
     return; // or maybe throw 
    } 
    var window = component.createObject(root) 
    window.show() 

} 

} 

Page1Form.ui.qml:

import QtQuick 2.7 
import QtQuick.Controls 2.0 
import QtQuick.Layouts 1.0 

Item { 
property alias textField: textField 
property alias button: button 


Rectangle { 

    id: rectangle 
    x: 0 
    y: 0 

    width: 1000 
    height: 800 

    gradient: Gradient { 
     GradientStop { 
      position: 0.031 
      color: "#ffffff" 
     } 

     GradientStop { 
      position: 0.901 
      color: "#000000" 
     } 


    } 

    RowLayout { 
     anchors.horizontalCenter: parent.horizontalCenter 
     anchors.topMargin: 20 
     anchors.top: parent.top 

     TextField { 
      id: textField 
      placeholderText: qsTr("ENTRER TEXTE") 
     } 

     Button { 
      id: button 
      text: qsTr("OK") 


     } 


    } 


} 
} 
+0

請提供QML代碼 – folibis

+0

這是我的主要qml – karahman

+0

爲什麼你需要那個? 「根對象」(ApplicationWindow)已經將它的'visible'屬性設置爲'true'。在大多數情況下,到達QML引擎並取出對象引用 –

回答

0

我這樣做是這樣的:

app = QGuiApplication(sys.argv) 
engine = QQmlApplicationEngine() 
ctx = engine.rootContext() 
ctx.setContextProperty("qmlapp", engine) #the string can be anything 
engine.load('main.qml') 
win = engine.rootObjects()[0] 
win.show() 

但這不可能是你的問題。當我在QML中有#字符時,我確實看到了錯誤。例如,如果你不小心忘記引號,如下所示:

background: Rectangle { 
    id: rect 
    border.color: menuBorderColor 
    color: #AAA000 //forgot quotes 
} 

這將導致蟒蛇抱怨列表索引超出範圍。徹底檢查您的QML代碼是否有這樣的錯誤。

+0

謝謝你的回答 我解決了問題在於pyqt的版本問題,我使用5.4版本,然後對於QtQuick.Controls 2的工作,必須使用aminimum Pyqt 5.7。對於我的廣告語言,抱歉;) – karahman