4
votes

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
    }
1

1 Answers

7
votes

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.