Reputation: 370
I saw other people asking this question but none of what I tried worked. I am using PyQt 5.10.1.
Here is python code:
app = QGuiApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl("module/Layout.qml"))
print(view.rootContext())
print(view.findChild(QObject, 'launcherComponent'))
import pdb; pdb.set_trace()
sys.exit(app.exec())
And here is the QML code:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.2
import "calendar/resources" as CalendarComponent
import "weather/resources" as WeatherComponent
import "launcher/resources" as LauncherComponent
import Test 1.0
import Weather 1.0
import Calendar 1.0
import Launcher 1.0
ApplicationWindow {
id: appId
width: Screen.desktopAvailableWidth
height: Screen.desktopAvailableHeight
visible: true
modality: Qt.ApplicationModal
flags: Qt.Dialog
title: qsTr("NarcisseOS")
color: "black"
LauncherComponent.LauncherComponent {
id: launcherComponentId
objectName: launcherComponent
height: parent.height
width: parent.width
anchors.centerIn: parent
}
}
I tried everything I thought about. But this findChild function is only returning None.
I tried to reinstall PyQt5. And I tried to put the objectName property in a Rectangle object, I thought maybe a more generic one would be working. None of it worked.
Upvotes: 3
Views: 3977
Reputation: 243897
Your code has several errors:
objectName
property must be a string:LauncherComponent.LauncherComponent {
id: launcherComponentId
objectName: "launcherComponent"
height: parent.height
width: parent.width
anchors.centerIn: parent
}
ApplicationWindow
you should not use QQuickView
since ApplicationWindow
creates a toplevel and QQuickView
as well so you will have 2 toplevels and you are looking for the son of QQuickView
but not in the ApplicationWindow
children, so I recommend you modify your .py to:import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtQml import *
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(QUrl("module/Layout.qml"))
if len(engine.rootObjects()) == 0:
sys.exit(-1)
print(engine.rootObjects()[0].findChild(QObject, 'launcherComponent'))
sys.exit(app.exec_())
That is, you must use QQmlApplicationEngine
.
Upvotes: 2