首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyQt:如何从QQmlApplicationEngine获取根对象

PyQt:如何从QQmlApplicationEngine获取根对象
EN

Stack Overflow用户
提问于 2017-03-15 14:31:30
回答 1查看 3.9K关注 0票数 1

我尝试使用PyQt和QML显示动态对象,但是我得到了以下错误:

代码语言:javascript
复制
win = engine.rootObjects()[0] 

IndexError:列出超出范围的索引

下面是我的代码PyQt:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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")


        }


    }


 }
 }
EN

回答 1

Stack Overflow用户

发布于 2017-03-30 16:33:34

我是这样做的:

代码语言:javascript
复制
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中有一个#字符时,我确实看到了错误。例如,如果您不小心忘记了下面的引号:

代码语言:javascript
复制
background: Rectangle {
    id: rect
    border.color: menuBorderColor
    color: #AAA000  //forgot quotes
}

这将导致python抱怨列表索引超出范围。彻底检查您的QML代码,以找出此类错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42812703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档