(Python) Cannot call findChild(QObject, 'child') on QObject. Returns None.



  • Sorry if this is not the appropriate section, I noticed there is a bindings section, but not many people seem to visit over there. Basically, I really love QT from what I've used so far, but with Python I've run into issues, mainly with connecting to signals, and finding children with the function findChild.

    Unfortunitely, I don't have much time, so I would like to know whether this is an issue with PyQt, or if there is a solution/workaround I can use for the time being. If I have to, I'll go backroll into Qt4 for what I'd like to do.

    Also ignore the ugly qml file, I've been too stuck on this issue to make it look pretty.

    I also have problems of crashes of the window emulation when using the connections tab of the designer in QT Creator, but I'll post a separate thread about that.

    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 ## I've also experimented in how this is implemented, but I can't get it to work any way I try
        print(login_window.findChild(QObject, 'white_background'))
        
        ## vvvv Doesn't connect ##
        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()
    
    import QtQuick 2.4
    import QtQuick.Controls 1.5
    
    Item {
        id: window
        objectName: window
        x: 0
        height: 350
        visible: true
        transformOrigin: Item.Center
        rotation: 0
    
        signal open_main_app(string username, string password)
    
        Rectangle {
            id: white_background
            objectName: white_background
            x: 0
            y: 0
            width: 640
            height: 350
            color: "#ffffff"
            border.width: 0
    
    print(login_window.findChild(QObject, 'white_background'))
            Rectangle {
                id: green_background
                x: 0
                y: 0
                width: 640
                height: 138
                color: "#30945d"
            }
    //your code here
            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
                }
            }
    
        }
    
    }
    

    So the problem is this returns None, despite there being an object of that name.

    print(login_window.findChild(QObject, 'white_background'))
    

    And this, when the button is pressed, not calling the connected function.

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

    Again, I'm sorry if this is the wrong section to have posted this, I just would like some help, as I really do love the flexibility this toolkit has to offer, and as my Stack Overflow thread went silently undread.