好的,你只需要創建一個對話框(或者看起來像一個對話框的組件),並且只想與主窗口和交互對話框窗口。
請嘗試以下方法:
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
id: rootWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
color: "green"
Rectangle {
id: behind
anchors.fill: parent
color: Qt.rgba(0, 0, 0, 0.7)
visible: false
}
MouseArea {
enabled: behind.visible
anchors.fill: parent
onClicked: {
console.log("Root Window")
}
}
Button {
text: "Open Dialog"
onClicked: {
behind.visible = true;
var comp = Qt.createComponent("qrc:/MyDialog.qml");
// var comp = Qt.createComponent("qrc:/DialogQt.qml");
var obj1 = comp.createObject(rootWindow, {});
obj1.z = 2;
}
}
}
MyDialog.qml
import QtQuick 2.7
Rectangle {
id: modalWindow
width: 200
height: 200
color: "red"
anchors.centerIn: parent
MouseArea {
anchors.fill: parent
onClicked: {
console.log("Modal Window")
}
}
}
點擊 '打開對話框' 按鈕將創建並打開 '模式' 對話框主窗口組件的頂部。
當然,您必須調整「MyDialog.qml」文件以適合您自己的設計要求。
但是,使用此作爲一個「真正的」對話也沒有工作,(我)像通用已經在評論部分中指出:
DialogQt.qml
Dialog {
visible: true
title: "Blue sky dialog"
modality : Qt.ApplicationModal
contentItem: Rectangle {
color: "lightskyblue"
anchors.fill: parent
Text {
text: "Hello blue sky!"
color: "navy"
anchors.centerIn: parent
}
}
}
也許你可以用MouseArea過濾掉對話外的點擊事件? – sk2212
@ sk2212:我認爲這不會起作用。我希望使用它能夠點擊並與主窗口或浮動遠程進行交互。基本上只是標準的工具窗口行爲。 – MusiGenesis
嗯......你可以從你的窗口和對話框創建一個簡單的截圖嗎?我仍然不明白你的觀點。 – sk2212