查看完整版本: [-- 关于qml的tableview checkbox 全选 --]

QTCN开发网 -> Qt QML开发 -> 关于qml的tableview checkbox 全选 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

dyongfly 2018-12-04 18:34

关于qml的tableview checkbox 全选

qml tableview 全选,部分代码如下MVC:

qml:
  1. 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++:
  1. 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();
    }
  1. 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代码,麻烦懂的朋友指导下,这个问题困扰我很久了


dyongfly 2018-12-04 18:35
这是全部代码[attachment=19709]

yhgaven 2018-12-28 15:16
我试了你demo是正常的啊,先选中一个,然后全选,就全是打钩的,再反选也是全是没打钩的。

lsyzsl 2019-04-18 14:05
楼主,问题解决了么?

kettong 2019-06-24 10:47
这个通过自定义属性的changed信号来写就可以。


查看完整版本: [-- 关于qml的tableview checkbox 全选 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled