Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make QAbstractTableModel with qml tableview checkable

How to make QAbstractTableModel with qml tableview checkable

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jtel
    wrote on last edited by
    #1

    Hello,

    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
    @
    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 FilterRole:
            return model.name();
        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

        TableViewColumn {
            role: "name"
            title: "Name"
            width: 200
        }
        TableViewColumn {
            role: "description"
            title: "Description"
            width: 100
        }
        TableViewColumn {
            role: "filter"
            title: "Filter"
            width: 100
        }
        TableViewColumn {
            role: "type"
            title: "Type"
            width: 100
        }
    

    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      IMO, you must create your own delegate with a CheckBox and Text elements and set the CheckBox accordingly. See "itemDelegate":http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop

      157

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jtel
        wrote on last edited by
        #3

        Hi,

        Thank you for your help. I used a delegate checkbox like this :

        @
        Component {
        id: checkBoxDelegate

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

        @

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

        And I modified data like this:

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

        But when I clicked on checkbox, data never updated.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved