• 138阅读
  • 1回复

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

上一主题 下一主题
离线dyongfly
 

只看楼主 倒序阅读 楼主  发表于: 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代码,麻烦懂的朋友指导下,这个问题困扰我很久了

离线dyongfly

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