[SOLVED] QTableView, QAbstractTableModel, and QStyledItemDelegate: Row color update not working.



  • I am attempting to toggle a checkbox in my first column that will toggle the entire row's color accordingly.

    Let's start off with the basics.

    I'm using a QTableView as my view, with the default QStyledItemDelegate that the QTableView uses.

    I've inherited QAbstractTableModel and have overloaded the following.

    @
    bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
    {
    Q_UNUSED(role);

     auto myClass = this->myClassList[index.row()];
    
     switch(index.column())
     {
          case Toggle:
               myClass->setToggle(value.toInd() == Qt::Checked);
               break;
     }
    
     this->dataChanged(index, index);
    
     return true;
    

    }

    QVariant MyModel::data(const QModelIndex& index, int role) const
    {
    QVariant value;

     auto myClass = this->myClassList[index.row()];
    
     if(role == Qt::DisplayRole || role == Qt::EditRole)
     {
          switch(index.column())
          {
               // Qt::ItemIsEditable columns
          }
     }
     else if(role == Qt::CheckStateRole)
     {
          switch(index.column())
          {
               case Toggle:
                    value = myClass->getToggle() ? Qt::Checked : Qt::Unchecked;
                    break;
          }
     }
     else if(role == Qt::BackgroundRole)
     {
           // Color the entire row red if checked
           value = myClass->getToggle() ? QColor(Qt::red) : QColor(Qt::white);
     }
    
     return value;
    

    }

    Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
    {
    Qt::ItemFlags flags(Qt::ItemIsSelectabled | Qt::ItemIsEnabled);

     switch(index.column())
     {
          case Toggle:
               flags |= Qt::ItemIsCheckable;
               break;
     }
    
     return flags;
    

    }
    @

    The issue I am having is, when I check my box in the checkable column, the row doesn't change color immediately.
    When a window (any Windows window) covers up my table, the table will refresh and the row will be red. When i move the cell selector with an arrow key (the cell turns selection blue) the cell will turn red.

    Now, when I change the Checkbox column to an editable column, I can type "true" or "false" and when i hit enter, the entire row will turn red as expected. So the issue is occuring when I use the checkbox itself (not the textbox to the right of the checkbox)

    Looking at the source code for QStyledItemDelegate, I see that no editor widget is created for when checking the checkbox. This means no commitData() is ever called. That's the only difference I can see between using the checkbox and the textbox.

    I even tried connecting a slot to the dataChanged() signal to "update()" the QTableView, didn't work.

    Am I doing something wrong? Is there a workaround that I could use for this case?

    Edit: Added examples to better show what i'm talking about.

    Unchecked, default table:
    !http://i.imgur.com/T1rBAp4.png(No Check)!

    I checked the box and moved the selector over a couple of cells:
    !http://i.imgur.com/LepmSuN.png(Checked, with moved selection)!

    I checked the box and the row was colored AFTER I resized my window.
    !http://i.imgur.com/nCVVL6I.png(Checked, some event caused a refresh)!


  • Moderators

    Qt itemviews are optimized in a way that they only repaint what's necessary.
    In your case only YOU know whats necessary to be repainted because you don't tell the view that there is a logical connection between the checkbox and the whole row.

    You you either

    call view->update( QmodelIndex(..) )

    emit dataChanged() (from the model)

    for every index in the row QModelIndex(r,1) to QModelIndex(r,c) when the checkbox is toggeled.
    Usually you need to do this on the setData() method of your model ... like you already do but only for the affected index itself.



  • Thank you, I didn't realize the paints weren't constantly being called. I tried calling an update on dataChanged but it didn't appear to work at the time.

    I'll see what I can come up with.


  • Moderators

    emitting dataChanged will automatically result in an repaint of the index by the view.

    So in your setData() method you should emit the dataChanged signal like this (for a table view):
    @
    emit dataChanged( index(index.row(),0), index(index.row(), columnCount()-1) );
    @



  • This was the case. I was only telling the view to update the cell that changed, but as I needed to update the entire row's color, I had to emit the dataChanged on the entire row.

    @
    // Tell the view to update the entire row
    auto topLeft = this->createIndex(index.row(), 0);
    auto bottomLeft = this->createIndex(index.row(), index.column() - 1);
    this->dataChanged(topLeft, bottomLeft);
    @

    Thank you.


Log in to reply
 

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