2016-07-27 78 views
1

讓我們假設我有一張使用Rectangle製作的卡片,並且我想在單擊時在其上顯示按鈕。我打電話showMenu()功能來做到這一點,併爲我使用動態ListModelListView按鈕。這樣的問題是,按鈕被添加到Rectangle而不是頂部。將項目追加到模型後,錨點不會更新。這是我的代碼ListView動態錨定

Item { 
    width: 120 
    height: 120 
    Rectangle { 
     id: card 
     width: 50 
     height: 100 
     color: "pink" 
     anchors.bottom: parent.bottom 
     Item { 
      id: rec 
      width: 50 
      anchors.bottom: parent.top // This anchor is not updating after appending an item to the list. 

      ListModel { 
       id: menuListModel 
      } 

      Component { 
       id: delegate 
       Rectangle { 
        width: 120 
        height: 20 
        color: "blue" 
        Text { 
         anchors.fill: parent 
         anchors.centerIn: parent 
         text: commandText 
        } 
       } 
      } 

      ListView { 
       anchors.fill: parent 
       model:menuListModel 
       delegate: delegate 
       interactive: false 
      } 
     } 

     MouseArea { 
      anchors.fill: parent 
      onClicked: menuListModel.append({"commandText" : "Normal Summon"}); 
     } 
    } 
} 
+0

你可以發佈運行的代碼嗎? – Mitch

+0

更新了示例。 –

回答

0

這或多或少是this question的副本。 Item需要height。正如在這個問題的答案中提到的那樣,當這樣的事情發生時,您可以向代碼添加調試語句。在這種情況下,你還可以添加一個Rectangle作爲Item的孩子,並確保它是可見:

Rectangle { 
    anchors.fill: parent 
    color: "transparent" 
    border.color: "darkorange" 
} 

如果它是不可見的,你知道,問題在於與(父)項目。

+0

添加高度將解決當前的例子,但在我的代碼中,高度是動態的,所以每次將對象附加到模型時,我應該不斷更新高度?或者有沒有什麼辦法可以讓ListView的高度從頂部更新而不是底部? –

+0

所以項目的高度取決於'ListView'的高度? – Mitch

+0

「Item」的高度取決於ListView的高度。 –