1
votes

I’m new in QT. I want to make every row of qml tableview checkable but it doesn’t work.

The tableview with data is shown successfully but it’s not checkable.

It seems that flags() and setData() functions are never run and role==Qt::CheckStateRole never be true.

Please help.

C++ code

I modified code like but it doesn't work:

    QVariant TableModel::data(const QModelIndex & index, int role) const {

    if (index.row() < 0 || index.row() >= _fields->size())
        return QVariant();

    if(role == Qt::CheckStateRole) {
        return rowsChk.contains(index.row()) ? Qt::Checked : Qt::Unchecked;
    }

    switch(role) {
        case NameRole:
            return model.name();
        case DescriptionRole:
            return model.description();
        case TypeRole:
            return model.type();
    }

    return QVariant();
}

bool TableModel::setData(const QModelIndex & index, const QVariant & value, int role){
         rowsChecked(index.row(), value) ;
         emit dataChanged(index, index);
        return true;
}

here is my qml file

    TableView {
        model: tableModel
        anchors.fill: parent
        frameVisible: true
        headerVisible: true
        sortIndicatorVisible: false
        alternatingRowColors: true

        Component {
            id: checkBoxDelegate

            Item {
                CheckBox {
                     anchors.fill: parent
                     checked: styleData.value
                 }
             }
        }

        TableViewColumn {
            role: "check"
            title: ""
            width: 30
            delegate: checkBoxDelegate
        }


        TableViewColumn {
            role: "name"
            title: "Name"
            width: 200

        }
        TableViewColumn {
            role: "description"
            title: "Description"
            width: 100
        }

        TableViewColumn {
            role: "type"
            title: "Type"
            width: 100
        }
1

1 Answers

0
votes

The setData() method is not called because you probably haven't reimplemented method:

QHash<int, QByteArray> roleNames() const;

which is a virtual method of QAbstractItemModel, the class that QAbstractTableModel inherits. Without this the QML does not know what your roles mean and does not care about your data. In your case this method should be defined this way:

QHash<int, QByteArray> FieldModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[DescriptionRole] = "description";
    roles[FilterRole] = "filter";
    roles[TypeRole] = "type";
    return roles;
}

Personally, I have no idea why the

Qt::ItemFlags flags(const QModelIndex &index) const;

method is not called - have the same problem. But you can solve it by defining a delegate of TableViewColumn in your QML:

Component {
    id: checkBoxDelegate

Item {
    CheckBox {
        anchors.fill: parent
        checked: styleData.value
    }
}
}

and then assign this delegate to your columns:

  TableViewColumn {
            role: "name"
            title: "Name"
            width: 200
            delegate: checkBoxDelegate
        }

Then the column contains a CheckBox control which value is specified by method

QVariant FieldModel::data(const QModelIndex & index, int role) const;

just return value 0 or QString("false") for unchecked and 1 or QString("true") for checked state.