2013-12-12 149 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 –