• 4443阅读
  • 4回复

[讨论]关于qml的tableview checkbox 全选 [复制链接]

上一主题 下一主题
离线dyongfly
 

只看楼主 正序阅读 楼主  发表于: 2018-12-04
qml tableview 全选,部分代码如下MVC:

qml:
  1. TableView{
  2.         id:tableview;
  3.         anchors.fill: parent;
  4.         model:tabModel;
  5.         frameVisible:false;
  6.         horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded
  7.         verticalScrollBarPolicy: Qt.ScrollBarAsNeeded
  8.         TableViewColumn{
  9.             role:"checked";
  10.             title: "";
  11.             delegate: Rectangle {
  12.                 CheckBox {
  13.                     anchors.centerIn: parent;
  14.                     checked: styleData.value;
  15.                     onClicked: {
  16.                         console.log(styleData.row);
  17.                         tabModel.setStatusByRow(styleData.row, checked);
  18.                     }
  19.                 }
  20.             }
  21.         }
  22.         TableViewColumn{role:"id"; title: "ID"; movable: false;horizontalAlignment: Text.AlignHCenter}
  23.         rowDelegate: Rectangle{
  24.             height: 40
  25.         }
  26.         headerDelegate: Rectangle{
  27.             implicitWidth: 10
  28.             implicitHeight: 40
  29.             Text{
  30.                 anchors.centerIn: parent;
  31.                 text: styleData.value
  32.                 color: styleData.pressed ?"#1AB2B5" : "#707070"
  33.                 font{
  34.                     family: qsTr("Arial");
  35.                     pixelSize: 14;
  36.                     bold: true
  37.                 }
  38.                 elide: Text.ElideRight
  39.             }
  40.             CheckBox{
  41.                 property bool myPressed: styleData.pressed
  42.                 anchors.centerIn: parent
  43.                 visible: styleData.column === 0
  44.                 activeFocusOnPress:true;
  45.                 onMyPressedChanged: {
  46.                     if(myPressed && styleData.column === 0)
  47.                     {
  48.                         checked = !checked;
  49.                         tabModel.selectAll(checked);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.     }
C++:
  1. QVariant TableModel::data(const QModelIndex &index, int role) const
  2. {
  3.     if (!index.isValid()/* || index.row() >= m_data.size()*/)
  4.         return QVariant();
  5.     if(role == Qt::CheckStateRole) {
  6.         return m_data.at(index.row())->checkstate;
  7.     }
  8.     if(role == Qt::UserRole)
  9.     {
  10.         return m_data.at(index.row())->id;
  11.     }
  12.     return QVariant();
  13. }
  1. void TableModel::selectAll(bool checked)
  2. {
  3.     beginResetModel();
  4.     for(int i = 0; i < m_data.size(); ++i)
  5.     {
  6.         m_data.at(i)->checkstate = checked;
  7.     }
  8.     endResetModel();
  9. }
问题:当启动程序后,点击表头的checkbox全选,反选,列中得checkbox状态一切正常。
但如果先点击某行中得checkbox,在选择全选或反选,则该行的check状态就不会刷新了,通过checkstatuschange信号看出,改行没有收到model中的信号,者是为什么呢?
附件是我的demo代码,麻烦懂的朋友指导下,这个问题困扰我很久了

离线kettong

只看该作者 4楼 发表于: 2019-06-24
这个通过自定义属性的changed信号来写就可以。
离线lsyzsl

只看该作者 3楼 发表于: 2019-04-18
楼主,问题解决了么?
离线yhgaven

只看该作者 2楼 发表于: 2018-12-28
我试了你demo是正常的啊,先选中一个,然后全选,就全是打钩的,再反选也是全是没打钩的。
离线dyongfly

只看该作者 1楼 发表于: 2018-12-04
这是全部代码 untitled2 3.zip (42 K) 下载次数:48
快速回复
限100 字节
 
上一个 下一个