1

I'm testing a custom popup menu strategy when I came across some odd behaviour: Requests to delete my dynamically created popup window are ignored.

Window {
    id: window
    width: 400
    height: 400
    color: "red"

    Rectangle {
        id: base
        width: 100
        height: 100
        anchors.centerIn: parent
        color: "green"

        property Window parentWindow: window
        property Window popup: null

        MouseArea {
            anchors.fill: parent
            onClicked: {
                base.popup = popupComp.createObject( null, { "parentWindow": base.parentWindow } );
            }
        }

        Connections {
            target: base.parentWindow
            onClosing: {
                if ( base.popup !== null ) {
                    base.popup.hide();
                    base.popup.destroy(); // 2
                }
            }
        }

        Component {
            id: popupComp

            Window {
                width: 150
                height: 150
                x: parentWindow.x + base.mapToItem( null, 0, 0 ).x
                y: parentWindow.y + base.mapToItem( null, 0, base.height ).y

                flags: Qt.Popup
                color: "blue"
                visible: true

                property Window parentWindow: null

                Component.onCompleted: requestActivate()

                Component.onDestruction: {
                    console.log( "Destroying popup" );
                }

                onActiveChanged: {
                    if ( !active ) {
                        console.log( "Popup inactive" );
                        hide();
                        base.popup = null;
                        destroy(); // 1
                    }
                }
            }
        }
    }
}

I have to create the popup window dynamically because it's the only way to specify no parent, as a subwindow's (i.e. a parented window) QWindow::active state seems to be dependent upon it's parent's.

Once the popup window is closed the popup's destroy() slot is called via the onActiveChanged handler - but object is not destroyed until the parent window's closing() signal is emitted. Here is the debug output from opening and closing the popup twice:

qml: Popup inactive
qml: Popup inactive
// Closing the parent window now
qml: Destroying popup
qml: Destroying popup

Any idea why the destroy() call at 1 is ignored whilst granted at 2?

1 Answer 1

2

I'd say it has something to do with it not being ready to process the call yet.

Along the lines of that guess, enforcing a delay before the destruction occurs seems to be a decent workaround:

thisWindow.destroy(1);
Sign up to request clarification or add additional context in comments.

1 Comment

Yep that's it, just setting it to 1 millisecond was sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.