I'm trying to make my python code interact with my QML file, but my findChild() function keeps returning None, I also tried findChildren() and it returned []. Browsed other questions but nothing I found worked. Here is my python code:
import sys
import os
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PyQt5 import QtCore, QtGui, QtQml
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join("ui", "main.qml"))
screen = engine.rootObjects()[0]
print(screen)
#returns <PySide2.QtGui.QWindow(0x1db42e7e2a0, name="main_window") at 0x000001DB434AD608>
print(screen.findChild(QtCore.QObject, "rec_body"))
#returns None
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
And the QML (gerenerated via QT Creator, I had to manually add the objectName but it didn't solve my problem):
import QtQuick 2.13
import QtQuick.Window 2.13
import QtQuick.Controls 2.15
Window {
id: main_window
objectName: "main_window"
width: 640
height: 560
visible: true
color: "#e0e0e0"
property alias rec_header: rec_header
property alias rec_body: rec_body
property alias img_logoSource: img_logo.source
title: qsTr("Hello World")
Rectangle {
id: rec_body
objectName: "rec_body"
x: 0
y: 49
width: 639
height: 510
color: "#ececec"
border.color: "#00000000"
Rectangle {
id: rec_start
x: 251
y: 458
width: 139
height: 41
color: "#5253ee"
radius: 8
border.color: "#5253ee"
border.width: 0
Text {
id: lbl_start
x: 53
y: 11
color: "#ffffff"
text: qsTr("Start")
font.pixelSize: 16
}
MouseArea {
id: msa_start
x: 0
y: 0
width: 139
height: 41
Connections {
target: msa_start
onClicked: console.log("??")
}
}
}
Text {
id: lbl_post
x: 511
y: 12
width: 49
height: 35
color: "#000000"
text: qsTr("Postos")
font.pixelSize: 16
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.family: "Tahoma"
}
Rectangle {
id: rec_result
x: 14
y: 294
width: 612
height: 151
color: "#ffffff"
radius: 8
border.color: "#00000000"
ScrollView {
id: scroll_result
x: 0
y: 0
width: 612
height: 151
}
}
Text {
id: lbl_action
x: 86
y: 12
width: 37
height: 35
color: "#000000"
text: qsTr("Ação")
font.pixelSize: 16
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.family: "Tahoma"
}
Text {
id: lbl_result
x: 282
y: 258
width: 71
height: 35
color: "#000000"
text: qsTr("Resultado")
font.pixelSize: 16
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.family: "Tahoma"
}
Text {
id: lbl_line
x: 297
y: 12
width: 41
height: 35
color: "#000000"
text: qsTr("Linha")
font.pixelSize: 16
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.family: "Tahoma"
}
Rectangle {
id: rec_line
x: 227
y: 47
width: 183
height: 200
color: "#ffffff"
radius: 8
ScrollView {
id: scroll_line
x: 0
y: 0
width: 183
height: 200
}
}
Rectangle {
id: rec_action
x: 14
y: 47
width: 183
height: 200
color: "#ffffff"
radius: 8
ScrollView {
id: scroll_action
x: 0
y: 0
width: 183
height: 200
}
}
Rectangle {
id: rec_post
x: 443
y: 47
width: 183
height: 200
color: "#ffffff"
radius: 8
border.color: "#00000000"
ScrollView {
id: scroll_post
x: 0
y: 0
width: 183
height: 200
}
}
}
Rectangle {
id: rec_header
x: 0
y: 0
width: 640
height: 48
color: "#5253ee"
border.color: "#00ffffff"
Image {
id: img_logo
x: -377
y: -107
width: 929
height: 262
source: "../../resources/logo_hor-03.png"
fillMode: Image.PreserveAspectFit
}
Rectangle {
id: rec_upaio
x: 49
y: 4
width: 200
height: 35
color: "#5253ee"
border.color: "#00000000"
Text {
id: lbl_value_version
x: 81
y: 17
width: 43
height: 19
color: "#ffffff"
text: qsTr("v0.0")
font.pixelSize: 10
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.family: "Tahoma"
}
Text {
id: lbl_upaio
x: 2
y: 2
width: 106
height: 35
color: "#ffffff"
text: qsTr("UpAIO")
font.pixelSize: 26
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.family: "Tahoma"
}
}
}
}
What am I missing?