Bind CheckBox checked-state in TableView to custom model attribute



  • 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();
    

    @


  • Moderators

    Hi,

    Did you try checked: styleData.value ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.