2013-12-12 69 views
4

下面我創建一個帶委託的ListView,其中包含一個CheckBox,其checked屬性綁定到該模型的checked角色。單擊代理時,我想通過更改模型的checked屬性來切換複選框狀態。但checkBox.checkedmodel.checked之間的綁定僅在用戶第一次單擊該代理時才起作用。之後,總是檢查checkBox,與model.checked值無關。結果是用戶不能取消選中複選框,我不想要這個。更改QML ListView的模型不會更改相應的代理

import QtQuick 2.2 
import QtQuick.Controls 1.1 

ListView { id: listView 
    height: 100 
    width: 100 

    model: ListModel { 
     ListElement { checked: false } 
     ListElement { checked: false } 
    } 

    delegate: Rectangle { 
     width: listView.width 
     implicitHeight: checkBox.implicitHeight * 1.3 

     CheckBox { id: checkBox 
      anchors.fill: parent 
      text: index + 1 
      checked: model.checked 
     } 

     MouseArea { id: mouseArea 
      anchors.fill: parent 
      onClicked: { 
       var item = listView.model.get(index); 

       console.log('old model.checked:', model.checked); 
       item.checked = !item.checked; 
       console.log('new model.checked:', model.checked); 

       console.log('checkBox.checked:', checkBox.checked); 
       console.log('something went wrong:', model.checked !== checkBox.checked); 
      } 
     } 
    } 
} 

問題在哪裏是我的代碼,我怎樣才能使委託工作像一個正常的CheckBox?

回答

6

這是一個報告的錯誤:https://bugreports.qt.io/browse/QTBUG-31627

使用該bug報告下的註釋中描述的解決方法使得我的代碼中的複選框正常工作。我刪除了checked: model.checked線和下面的代碼添加到矩形代表:

Binding { 
    target: checkBox 
    property: 'checked' 
    value: model.checked 
} 
+0

你說得對,我能再現一個最小項目的bug沒有任何型號:https://gist.github.com/webmaster128/7979803 –