简体   繁体   English

PySide2 findChild 不返回

[英]PySide2 findChild returns none

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 [].我试图让我的 python 代码与我的 QML 文件交互,但我的 findChild() function 一直返回 None,我也试过 findChildren() 它返回了[]。 Browsed other questions but nothing I found worked.浏览了其他问题,但没有发现任何问题。 Here is my python code:这是我的 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()

And the QML (gerenerated via QT Creator, I had to manually add the objectName but it didn't solve my problem):还有 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"
            }    
        }
    }    
}

What am I missing?我错过了什么?

The cause of the error is simple: You should not combine PySide2 with PyQt5 as they will generate silent bugs that are difficult to track down.错误的原因很简单:不应将 PySide2 与 PyQt5 结合使用,因为它们会产生难以追踪的静默错误。 "screen" is a PySide2 wrapper but you are indicating that it looks for a child of type QObject from the PyQt5 library which is illogical. “screen”是一个 PySide2 包装器,但您表示它从不合逻辑的 PyQt5 库中查找 QObject 类型的子代。

The solution is simple: Just use PySide2!!!解决方案很简单:只需使用 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()

Output: Output:

<PySide2.QtGui.QWindow(0x556c48b6d8b0, name="main_window") at 0x7fb7d87f4240>
<PySide2.QtCore.QObject(0x556c48f2f830, name = "rec_body") at 0x7fb7d87f4280>

Note: The way that you interact with QML is susceptible to errors, it is not recommended to export QML objects to / but vice versa as I point out in these posts:注意:您与 QML 交互的方式容易出错,不建议将 QML 对象导出到 / 这些帖子:反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM