python - 无法在 QObject 上调用 findChild(QObject, 'child' )。无返回值

标签 python python-3.x pyqt pyqt5 qt-quick

我最近一直在使用QTCreator,我爱上了ATM。不幸的是,我想将它与 Python 一起使用,但我遇到了问题。我遇到的最大问题是发现我的应用程序上下文的子级返回 None。

main.py

import sys

from PyQt5.QtCore import QUrl, QSize, QObject
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView


LOGIN_SCREEN_SIZE = QSize(640, 350)


def main():
    application = QGuiApplication(sys.argv)

    login_window = QQuickView()
    login_window.setSource(QUrl('loginscreen.qml'))

    login_window.setMinimumSize(LOGIN_SCREEN_SIZE)
    login_window.setMaximumSize(LOGIN_SCREEN_SIZE)

    login_window.show()

    ## vvvv Returns None ##
    print(login_window.findChild(QObject, 'white_background'))

    # login_window.rootObject().open_main_app.connect(app)

    sys.exit(application.exec_())


def app(username, password):
    print("YAY")

    main_window = QQuickView()
    main_window.show()


if __name__ == '__main__':
    main()

加载屏幕.qml

import QtQuick 2.4
import QtQuick.Controls 1.5

Item {
    id: window
    x: 0
    height: 350
    visible: true
    transformOrigin: Item.Center
    rotation: 0

    signal open_main_app(string username, string password)

    Rectangle {
        id: white_background
        x: 0
        y: 0
        width: 640
        height: 350
        color: "#ffffff"
        border.width: 0


        Rectangle {
            id: green_background
            x: 0
            y: 0
            width: 640
            height: 138
            color: "#30945d"
        }

        Text {
            id: login_text
            x: 0
            y: 0
            width: 640
            height: 124
            color: "#b3d756"
            text: qsTr("LOGIN")
            styleColor: "#f60000"
            style: Text.Normal
            font.bold: true
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 112
        }


        TextField {
            id: username_box
            x: 149
            y: 160
            width: 384
            height: 30
            placeholderText: qsTr("Steve")
        }


        TextField {
            id: password_box
            x: 149
            y: 215
            width: 384
            height: 30
            echoMode: 2
            placeholderText: qsTr("qwerty123")
        }


        Label {
            id: label1
            x: 40
            y: 166
            text: qsTr("Username")
        }


        Label {
            id: label2
            x: 40
            y: 221
            text: qsTr("Password")
        }


        Button {
            id: login_button
            x: 548
            y: 308
            text: qsTr("Go")

            onClicked:
            {
                window.open_main_window(username_box.text, password_box.text)

                //Qt.quit() << Only works when launching from C++ file
            }
        }

    }

}

white_background 不是我测试过的唯一一个,它们都不起作用。我不明白问题是什么。附带说明一下,我现有的信号也没有发送。我想尝试手动连接,但我不能,因为 findChild 不起作用。

最佳答案

这是因为 findChild 不按 id 查找元素。相反,您应该在 qml 文件中使用 objectName

将您的 white_background 矩形重写为:

Rectangle {
        id: white_background
        objectName: "white_background"
        x: 0
        y: 0
        width: 640
        height: 350
        color: "#ffffff"
        border.width: 0
        ...
}

关于python - 无法在 QObject 上调用 findChild(QObject, 'child' )。无返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36273655/

相关文章:

python - 从按键事件设置小部件

python - 是否可以弃用异常类?

Python 过滤 *args **kwargs 中的参数

python - 在 Python 中通过 NAT 中继/发送

python - 将qcombobox中所做的选择保存在pyqt中

python - 是否可以在 qml 中创建 map 网格?

Python 正则表达式没有返回任何重复的内容?

python - 如何使用 SQLAlchemy 只创建一张表?

arrays - 如何在不出现格式错误的数组错误的情况下从数组列中进行选择

Python:按理解访问列表