PySide2 findChild returns none
PySide2 findChild returns none
我试图让我的 python 代码与我的 QML 文件交互,但我的 findChild() 函数一直返回 None,我也尝试了 findChildren() 并且它返回了 []。浏览了其他问题,但我发现没有任何效果。这是我的 python 代码:
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()
还有 QML(通过 QT Creator 生成,我不得不手动添加 objectName 但它没有解决我的问题):
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"
}
}
}
}
我错过了什么?
错误的原因很简单:您不应将 PySide2 与 PyQt5 结合使用,因为它们会产生难以追踪的无声错误。 “screen”是 PySide2 包装器,但您表示它从 PyQt5 库中寻找类型为 QObject 的子项,这是不合逻辑的。
解决方法很简单:使用 PySide2!!!
import os
import sys
<b>from PySide2.QtCore import QObject</b>
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join("ui", "main.qml"))
screen = engine.rootObjects()[0]
print(screen)
print(screen.findChild(<b>QObject</b>, "rec_body"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
输出:
<PySide2.QtGui.QWindow(0x556c48b6d8b0, name="main_window") at 0x7fb7d87f4240>
<PySide2.QtCore.QObject(0x556c48f2f830, name = "rec_body") at 0x7fb7d87f4280>
注意:您与 QML 交互的方式容易出错,不建议将 QML 对象导出到 python/c++,反之亦然,正如我指出的在这些帖子中:
我试图让我的 python 代码与我的 QML 文件交互,但我的 findChild() 函数一直返回 None,我也尝试了 findChildren() 并且它返回了 []。浏览了其他问题,但我发现没有任何效果。这是我的 python 代码:
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()
还有 QML(通过 QT Creator 生成,我不得不手动添加 objectName 但它没有解决我的问题):
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"
}
}
}
}
我错过了什么?
错误的原因很简单:您不应将 PySide2 与 PyQt5 结合使用,因为它们会产生难以追踪的无声错误。 “screen”是 PySide2 包装器,但您表示它从 PyQt5 库中寻找类型为 QObject 的子项,这是不合逻辑的。
解决方法很简单:使用 PySide2!!!
import os
import sys
<b>from PySide2.QtCore import QObject</b>
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join("ui", "main.qml"))
screen = engine.rootObjects()[0]
print(screen)
print(screen.findChild(<b>QObject</b>, "rec_body"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
输出:
<PySide2.QtGui.QWindow(0x556c48b6d8b0, name="main_window") at 0x7fb7d87f4240>
<PySide2.QtCore.QObject(0x556c48f2f830, name = "rec_body") at 0x7fb7d87f4280>
注意:您与 QML 交互的方式容易出错,不建议将 QML 对象导出到 python/c++,反之亦然,正如我指出的在这些帖子中: