【问题标题】:Cannot call findChild(QObject, 'child') on QObject. Returns None无法在 QObject 上调用 findChild(QObject, 'child')。返回无
【发布时间】:2016-03-29 00:57:20
【问题描述】:

我最近一直在使用 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()

loadingscreen.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 不起作用。

【问题讨论】:

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


    【解决方案1】:

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

    white_background Rectangle 重写为:

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

    【讨论】:

    • 您好,感谢您的回复。我已经继续前进了,但是当我还在尝试这个时,我确实尝试了 objectName,但这也没有用。我发誓我确实在问题中尝试过,但我想我没有尝试过,时间太长了。
    【解决方案2】:

    在试图弄清楚如何从 pyqt 访问 TextInput 元素的值时遇到了这个问题。尝试按照 ismael 的建议设置 objectName 并且它有效。所以我猜伊斯梅尔是对的。

    这是我尝试过的有效方法。

    @pyqtSlot(result = str)
    def saveEdits(self):
        comments = appLabel.findChild(QObject, "commentsTextArea").property("text")
        print(comments)
        return "save edits success"
    

    其中appLabel = QQuickView()commentsTextAreaobjectNameTextInput 元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 2011-03-31
      • 1970-01-01
      • 2018-08-11
      • 2021-01-30
      • 1970-01-01
      相关资源
      最近更新 更多