karahman
karahman

Reputation: 21

PyQt: How to get the root object from QQmlApplicationEngine

I try to display a dynamic object using PyQt and QML, But I get this error:

win = engine.rootObjects()[0] 

IndexError: list index out of range

Here is my code 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: with a non-dynamic object, my code runs okay)

this is my 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")
    }
}

}

and my Page1.qml import 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")


        }


    }


 }
 }

Upvotes: 1

Views: 4511

Answers (1)

ak22
ak22

Reputation: 158

I am doing it this way:

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

But that is not likely your problem. I did see the error when I had a #character in the QML. For example, if you accidentally forget the quotes in the following:

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

This will cause python to complain about list index out of range. Check your QML code thoroughly for such mistakes.

Upvotes: 1

Related Questions