PySide2 findChild 不返回

Posted

技术标签:

【中文标题】PySide2 findChild 不返回【英文标题】:PySide2 findChild returns none 【发布时间】:2021-01-18 13:58:18 【问题描述】:

我正在尝试让我的 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"
                
        
        

我错过了什么?

【问题讨论】:

【参考方案1】:

错误的原因很简单:不应将 PySide2 与 PyQt5 结合使用,因为它们会产生难以追踪的静默错误。 “screen”是一个 PySide2 包装器,但您表示它从 PyQt5 库中查找 QObject 类型的子代,这是不合逻辑的。

解决方案很简单:只需使用 PySide2 !!!

import os
import sys

from PySide2.QtCore import QObject
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(QObject, "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++,反之亦然,正如我在这些帖子中指出的那样:

How to connect Python and QML with PySide2? How Can I Update a Qml Object's Property from my Python file?

【讨论】:

成功了!感谢您的回答以及进一步阅读的建议,它已经改变了我对连接主题的一些看法。

以上是关于PySide2 findChild 不返回的主要内容,如果未能解决你的问题,请参考以下文章

关于 QML 和 PySide2 的几个问题

QObject::findChild 为添加到状态栏的 QLabel 返回 0

如何修复 PySide2 QPixmapCache.find() DeprecationWarning?

findChild 使用自定义小部件返回 None

PyQt5 findChild返回None

PyQt5 和 QML 集成问题,“findChild”返回 AttributeError