Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Bind CheckBox checked-state in TableView to custom model attribute
Qt 6.11 is out! See what's new in the release blog

Bind CheckBox checked-state in TableView to custom model attribute

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 2.2k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Hedge
    wrote on last edited by
    #1

    I've got a QML-application containing a TableView with two columns. One of them is a CheckBox. Now I created a model derived from QAbstractTableModel. Reading data for the ordinary text-column already works but how do I sync the checked-property for my CheckBox with the model?

    Currently I can't even set it checked via model. You find the relevant code below.

    !http://i.stack.imgur.com/hzfO2.jpg()!

    tablemodel.cpp

    @TableModel::TableModel(QObject *parent) :
    QAbstractTableModel(parent)
    {
       list.append("test1");
       list.append("test2");
    }
    
    int TableModel::rowCount(const QModelIndex &parent) const
    {
       Q_UNUSED(parent);
       return list.count();
    }
    
    int TableModel::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return 2;
    }
    
    QVariant TableModel::data(const QModelIndex & index, int role) const {
    
    if (index.row() < 0 || index.row() >= list.count())
        return QVariant();
    
    if (role == NameRole)
        return list[index.row()]
    
    else if (role== EnabledRole){
       //list is not QList<QString>, its a custom class saving a String and a boolean for the checked state
       return list[index.row()].isEnabled();
       }
       else {
          return QVariant();
       }
    }
    
    QHash<int, QByteArray> TableModel::roleNames() const {
       QHash<int, QByteArray> roles;
       roles[NameRole] = "name";
       roles[EnabledRole] = "enabled";
       return roles;
    }@
    

    tablemodel.hpp

    @ class TableModel : public QAbstractTableModel
    {
    Q_OBJECT
    public:
    enum Roles {
    NameRole = Qt::UserRole + 1,
    EnabledRole
    };

    explicit TableModel(QObject *parent = 0);
    
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex & parent = QModelIndex()) const;
    Q_INVOKABLE QVariant data (const QModelIndex & index, int role) const;
    
    protected:
       QHash<int, QByteArray> roleNames() const;
    private:
       QList<QString> list;@
    

    main.qml

       @    TableView {
    
       id: Table
       model:  TableModel
    
       TableViewColumn {
          role: "name"
       }
    
       TableViewColumn {
          role: "enabled"
          delegate: CheckBox {
             //how to get the right state from the model
             //checked: ??
          }
       }
    }@
    

    main.cpp

    @QQmlApplicationEngine engine;
    QQmlContext * context = new QQmlContext(engine.rootContext());
    
    TableModel tableModel;
    context->setContextProperty("tableModel",&tableModel);
    
    QQmlComponent component(&engine, QUrl("qrc:///main.qml"));
    QQuickWindow * window = qobject_cast<QQuickWindow*>(component.create(context));
    window->show();
    

    @

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

      Hi,

      Did you try checked: styleData.value ?

      157

      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