0
當我遇到一些奇怪的行爲時,我正在測試自定義彈出菜單策略:刪除我動態創建的彈出窗口的請求將被忽略。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
任何想法,爲什麼同時在2
授予destroy()
致電1
被忽略?
沒錯,就是這樣,只是將其設置爲1毫秒就足夠了。 – cmannett85 2014-10-27 17:47:56