Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't close the window with button: QML

Tags:

qt

qml

I've created an item, that contains a button. I'm trying to close parent window of item with this button, but I'm getting this message, when click the item:

TypeError: Property 'close' of object QQuickRootItem(0x1d8efed8) is not a function

Can you help me with this?

Code of item:

import QtQuick 2.4

Item {

    id: backButton

    ItemForButton{

        id: baseButton
        text: "Back"

        onClicked: {

            backButton.parent.close()
        }

    }

}

Code for window:

Window {

        id: window
        visible: true
        BackButton {

        }
        x: 30
        y: 30
    }
like image 443
Nazarii Plebanskii Avatar asked Mar 16 '23 03:03

Nazarii Plebanskii


1 Answers

That seems a bit messy. If I were you, I'd add a clicked signal to the custom button type. For example:

Item:

import QtQuick 2.4

Item {
    id: backButton

    // Add a clicked signal here
    signal clicked()

    ItemForButton{

        id: baseButton
        text: "Back"

        onClicked: {
            // Emit the new clicked signal here:
            backButton.clicked();
        }
    }
}

Window:

Window {
    id: window
    visible: true

    BackButton {
        // Respond to the signal here.
        onClicked: window.close();
    }
}

This provides the flexibility of using your custom BackButton type in other ways in the future.

like image 83
MrEricSir Avatar answered Mar 23 '23 01:03

MrEricSir