dyongfly |
2018-12-04 18:34 |
关于qml的tableview checkbox 全选
qml tableview 全选,部分代码如下MVC:
qml:
- TableView{
id:tableview; anchors.fill: parent; model:tabModel; frameVisible:false; horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded verticalScrollBarPolicy: Qt.ScrollBarAsNeeded TableViewColumn{ role:"checked"; title: ""; delegate: Rectangle { CheckBox { anchors.centerIn: parent; checked: styleData.value; onClicked: { console.log(styleData.row); tabModel.setStatusByRow(styleData.row, checked); } } } } TableViewColumn{role:"id"; title: "ID"; movable: false;horizontalAlignment: Text.AlignHCenter} rowDelegate: Rectangle{ height: 40 } headerDelegate: Rectangle{ implicitWidth: 10 implicitHeight: 40 Text{ anchors.centerIn: parent; text: styleData.value color: styleData.pressed ?"#1AB2B5" : "#707070" font{ family: qsTr("Arial"); pixelSize: 14; bold: true } elide: Text.ElideRight } CheckBox{ property bool myPressed: styleData.pressed anchors.centerIn: parent visible: styleData.column === 0 activeFocusOnPress:true; onMyPressedChanged: { if(myPressed && styleData.column === 0) { checked = !checked; tabModel.selectAll(checked); } } } } }
C++:
- QVariant TableModel::data(const QModelIndex &index, int role) const
{ if (!index.isValid()/* || index.row() >= m_data.size()*/) return QVariant(); if(role == Qt::CheckStateRole) { return m_data.at(index.row())->checkstate; } if(role == Qt::UserRole) { return m_data.at(index.row())->id; } return QVariant(); }
- void TableModel::selectAll(bool checked)
{ beginResetModel(); for(int i = 0; i < m_data.size(); ++i) { m_data.at(i)->checkstate = checked; } endResetModel(); }
问题:当启动程序后,点击表头的checkbox全选,反选,列中得checkbox状态一切正常。 但如果先点击某行中得checkbox,在选择全选或反选,则该行的check状态就不会刷新了,通过checkstatuschange信号看出,改行没有收到model中的信号,者是为什么呢? 附件是我的demo代码,麻烦懂的朋友指导下,这个问题困扰我很久了
|
|