python - PyQt:如何从 QQmlApplicationEngine 获取根对象

标签 python pyqt qml qqmlapplicationengine

我尝试使用 PyQt 和 QML 显示动态对象,但出现此错误:

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")


        }


    }


 }
 }

最佳答案

我这样做:

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
}

这将导致 python 提示列表索引超出范围。彻底检查您的 QML 代码是否存在此类错误。

关于python - PyQt:如何从 QQmlApplicationEngine 获取根对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42812703/

相关文章:

python - 使 Python 类在创建新属性时抛出错误

linux - 我应该如何在 Qt 中编写窗口管理器?

python - 从 Pandas 的一系列线条中制作列

python - 使用 pandas 分析超过 20G 的数据帧,内存不足,指定 chunksize 时仍然无法正常工作

python - 如何使用QPainterPath?

qt - PyQt:QStandardItemModel:如何获取一行作为列表?

python - 如何在 PyQt5 InputDialog 中获得更多输入文本?

javascript - 如何在 QWebEngineView 中将值从 QML 传递给 JavaScript

qml - 创建一个组件作为对象实例中的成员

python - 使用多线程(使用并发.futures)在Python中添加列表会导致偏移行