[Solved]Get value of checkbox in a qtableview

  • Hello
    I need some help to get the checked value in a tableview.

    My code looks like this
    @ QModelIndexList selectedList= ui->testTable->selectionModel()->selectedRows(5);//colum som läggs till listan
    QModelIndex index = ui->testTable->currentIndex();

    QBrush cyan;
    foreach (index,selectedList )
        if (dataModel->data(index))
          dataModel->setData(index, cyan, Qt::BackgroundRole);


    But i need to loop thrue the hole datamodel and check every box.

    A hint someone

  • Hi,

    from your code, it's not clear what you want to achieve. It does not fir to the question.
    Inside the code, you iterate all selected cells in column 5 and set the background color. Where comes the checked state into the game?

  • If you want to query the checked state of a model, simply use the correct data role Qt::CheckStateRole, which returns a QVariant containing a Qt::CheckState.

    However, it seems that you want to simply try to modify the rendering of checked items to give them a background color. That can be done more efficiently using a [[doc:QItemDelegate]].

  • bq. However, it seems that you want to simply try to modify the rendering of checked items to give them a background color. That can be done more efficiently using a QItemDelegate.

    I want to check the state of the checkbox and it is in the 5 th column.
    if the state is true/checked I want to change color of that row.

    And i also want to se the total rows in the model.
    Is it possible.

    I'd like some simple exampels if there are some around.

  • For the background, use a [[Doc:QStyledItemDelegate]] subclass, as Andre suggested. Override the paint() method, check the row/column and data and put the changed background into the options and call the base class implementation in the end:

    void MyItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index )
    QStyleOptionViewItemV4 newOption(option);
    if(index.row() == 5) {
    if(index.data(Qt::CheckStateRole) == Qt::Checked) {
    newOption.backgroundBrush = QBrush(Qt::yellow);
    QStyledItemDelegate::paint(painter, newOption, index);

    (Brain to terminal, code is not tested)

    You get the total number of rows using model->rowCount().

  • Thanks I'll try

  • Yes i worked but i want to loop thru the datamodel and check all checkboxes.
    I thought i did that with my foreach but sometimes i got the right rowcount and the loop dosent work and sometimes i go thru the loop but only on one row.

  • The loop is executed by the view, it calls the delegate with the needed indexes.

  • I'll explain this in a new way.
    After i put all my values in the model()(QStandardItemModel)
    I put the model in the tableview.

    When this is done I try to loop thru the datamodel and tries to check if the

    is checked.

    If so I want to change color of that row.

    thats why I want to loop thru this.

    I'm sure that this is a simple task but I'm stuck now

  • OK, so you want to color a complete row based on a set of conditions. Sounds like a job for a proxy model to me.

    Something like:

    //in Qt 4.8, you should use QIdentityProxyModel (I think it is called, please check).

    #if QT_VERSION >= 0x040800
    #include <QIndentityProxy>
    class HighLightProxy : public QIdentityProxyModel
    typedef baseClass QIdentityProxyModel;
    #include <QSortFilterProxyModel>
    class HighLightProxy : public QSortFilterProxyModel
    typedef baseClass QSortFilterProxyModel;

    HighLightProxy(QObject* parent = 0);

    void setRow(int row);
    void setCheckColumn(column);
    void setHighlightBrush(const QBrush& brush);
    virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole ) const;

    int m_row;
    int m_checkColumn;
    QBrush m_brush;

    HighLightProxy::HighLightProxy(QObject* parent) :

    QVariant HighLightProxy::data(const QModelIndex & index, int role ) const
    if (!index.isValid())
    return QVariant();

    //lets assume you are not talking about a tree-type model here, and ignore parents
    if (index.row() == m_row && role == Qt::BackGroundRole) {
    //check the checkstate
    if (m_checkColumn > -1) {
    QModelIndex sIndex = sourceModel()->index(m_row, m_checkColumn);
    if (sIndex.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
    return m_highlightBrush;
    return baseClass::data(index, role);

    //implementation of setters declared in header is trivial and thus not shown.

    Note: written in forum editor, not complete, nor tested in a compiler.

  • I'll try this but I'm stuck with how to loop thru the tableview or datamodel to.
    A simpel loop but Not in my head for the moment.

    pls help

  • With the approach above, you don't need to loop through the view or the model.

  • Thanks for the code and its OK

    No problems after a while.

Log in to reply