模式 Window 不工作

Modal Window not working

我在 QML 中有一个主 window,当我 button 上单击 时,我想要那个 modal window 可以弹出。

我试过了,但是 it is not working 我不明白为什么:

Rectangle {
    id: root

    ...

    Button {
        onClicked: {
            var factory = Qt.createComponent( "DialogWindow.qml" );
            var dialog = factory.createObject(root);
            dialog.show();
        }
   }
}

main.qml

Window { //I also tried "Dialog"
    height: 240
    width: 320
    title: qsTr( "Popup" )
    visible: true
    modality: 1

    ListView{
        //My stuff
    }
}

DialogWindow.qml

这里是错误:

TypeError: Cannot call method 'show' of null

我做错了什么?

如果您阅读 modal 的 属性 文档,它会说

Possible values are Qt.NonModal (the default), Qt.WindowModal, and Qt.ApplicationModal.

那么为什么不使用它们而不是硬编码 0 或 1?

modality: Qt.ApplicationModal