2017-01-24 54 views
0

我有一個Flow佈局,我在用戶動作上動態添加項目。以同樣的方式,我在用戶操作中刪除這些項目。流程QML組件似乎按預期工作,直到項目被刪除。該項目本身被刪除,但它佔用的空間只是空白。我的直覺告訴我,圖形項目本身被刪除,但當項目被移除時,視圖不會更新。當項目被刪除時可以更新/刷新流程QML組件嗎?

動態刪除流項目範圍之外的子項目嗎?是否有任何其他佈局行爲平等? GridLayout似乎是最接近的,但它不會在佈局調整大小時自動換行子項。

是否有任何非黑客方式來啓用流重新排列時,子項被禁用?如果沒有,並且如果GridLayout是我的最佳鏡頭,那麼如何使它像Flow一樣包裝它的子項目呢?

下面的代碼演示我想達到的目標:

Item { 
    id: root 

    Flow { 
     id: layout 
     anchors.fill: parent 

     Loader { id: loader } 
    } 

    MouseArea { 
     anchors.top: parent.top 
     height: parent.height/2 
     width: parent.width 
     onClicked: loader.source = "SomeQmlComponent.qml" 
    } 

    MouseArea { 
     anchors.bottom: parent.bottom 
     height: parent.height/2 
     width: parent.width 
     onClicked: loader.source = "" 
    } 
} 
+0

如何刪除項目?請提供您的代碼... – folibis

+0

@folibis:我目前使用一個Loader並對其源屬性進行操作。我知道不應該刪除使用加載器實例化的組件,但我認爲這不是問題。 – Phat

回答

1

不要使用LoaderFlow。在你的情況下,項目的父項爲Loader,而不是Flow,所以你失去了所有優勢。在正常的方式項目添加和刪除沒有問題:

import QtQuick 2.7 
import QtQuick.Window 2.2 

Window { 
    width: 600 
    height: 600 
    visible: true 

    Component { 
     id: element 
     Rectangle { 
      width: Math.round(Math.random() * 100) + 50 
      height: Math.round(Math.random() * 100) + 50 
      color: Qt.rgba(Math.random(),Math.random(),Math.random(),1) 
     } 
    } 

    Flow { 
     id: flow 
     spacing: 2 
     anchors.fill: parent 
     add: Transition { 
      NumberAnimation { properties: "x,y"; easing.type: Easing.OutBack } 
     } 
     move: add 
    } 

    Timer { 
     id: timer 
     property bool is_add: true 
     interval: 300 
     repeat: true 
     running: true 
     onTriggered: { 
      if(timer.is_add) { 
       element.createObject(flow); 
       if(flow.children.length > 20) { 
        timer.is_add = false; 
       } 
      } else { 
       var item = flow.children[0]; 
       item.destroy(); 
       if(flow.children.length <= 1) { 
        timer.is_add = true; 
       } 
      } 
     } 
    } 
} 
+0

有沒有一些方法可以在開始時插入項目而不是稍作修改?看起來像createObject()不提供這樣的選項。 – Phat

+1

這是可能的。由於'Item.children'只是一個項目數組,因此'list '您可以根據需要構建列表,因此將該數組重新分配給'Item.children' – folibis

+0

但是我必須首先按照您的示例,然後重新排列後?這可能會弄亂動畫? – Phat

相關問題