QML动态创建对象生存期

时间:2014-10-27 16:28:28

标签: qt qml

当我遇到一些奇怪的行为时,我正在测试自定义弹出菜单策略:忽略删除动态创建的弹出窗口的请求。

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

我必须动态创建弹出窗口,因为它是指定无父级的唯一方法,因为子窗口(即父级窗口)QWindow::active状态似乎依赖于它的父级。

一旦弹出窗口关闭,弹出窗口的destroy()插槽就会通过onActiveChanged处理程序调用 - 但是在发出父窗口的closing()信号之前,对象不会被销毁。以下是打开和关闭弹出窗口两次的调试输出:

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

在[{1}}授予destroy()时,忽略1 2 {{1}}来电的原因?

1 个答案:

答案 0 :(得分:2)

我说这与它尚未准备好处理通话有关。

根据猜测,enforcing a delay before the destruction occurs似乎是一个不错的解决方法:

thisWindow.destroy(1);
相关问题