0
我試圖刪除QML對象並重新創建對象是這樣的:如何刪除QML對象
Rectangle{
property var obj
signal videoStopped(variant complete)
function recreate(url){
if(!obj){
console.log("createObject")
obj = videoComponet.createObject(root)
obj.stopped.connect(function(){
videoStopped(obj.status == MediaPlayer.EndOfMedia)
})
}
obj.source = url
obj.play()
}
function stop(){
obj.destroy() // obj.deleteLater()
}
Component{
id: videoComponet
Video {
anchors.fill: parent
visible: true
autoPlay: true; autoLoad: true
}
}
}
C++端調用recreate
生成一個對象並調用stop
將其刪除。
recreate
⇒控制檯輸出的CreateObject↓
stop
↓
recreate
⇒控制檯沒有輸出
obj.destroy()
和obj.deleteLater()
都沒有工作。 如何強制刪除動態創建的對象,就像在C++中刪除一樣。
有你打過電話後消滅的obj設置爲null?我也會給它一個null屬性var obj的初始值:null –
Creator有一個QML調試器,你可以檢查'obj'的值,或者你可以打印出來。 – Mitch
添加如下內容:'Component.onDestruction:console.log('destroy')'到'Component'中的'Video'-Item和'console.log(obj)'作爲你函數的第一行重建(URL)'。如果你在鏈中的某個地方使用這個信號,還需要一個'onVideoStopped:console.log(complete)'。 – derM