Important: Please read the Qt Code of Conduct -

QTableWidget: repaint of specific cell

  • Hello!
    Is it possible to force a repaint of a specific cells?

    As I know,


    repaints all cells.

    I have tried to do this:

    QRect rrr = ui->tableWidget->visualItemRect(ui->tableWidget->item(myRow, myCol));

    But it is not working.

    This needs for color updating in 'paint' event of table delegate.

    It works if I update text only.

    Thank you!

  • Lifetime Qt Champion

    If you dont set any data/roles on the item, then view dont considered it "dirty"

    So how do you set this delegate color ?

    for test you can try

        auto model = qobject_cast<QAbstractTableModel *> ( ui->tableWidget->model() );
        if (!model) return;
        QModelIndex idx = model->index(myRow, myCol);
        if (idx.isValid())
            model->dataChanged(idx, idx,   QVector<int>({Qt::DisplayRole, Qt::EditRole}));

    with ->update(rrr);

    and see if its then repainted.

  • Hi!

    @mrjj said in QTableWidget: repaint of specific cell:

    So how do you set this delegate color?

    In paint section, I do color blinking of specific cells.
    The text is not changing.

    if(i == 0)
      brush = QBrush(QColor::yellow);
    else if(i == 1)
      brush = QBrush(QColor::red);
    painter->fillRect(rect, brush);
    painter->drawText(rect, Qt::AlignCenter, textCell);

    Something like this.

    Your code works also without executing 'update' method.
    Thank you!

  • Lifetime Qt Champion

    Ahh so its only in the delegate so view doesn't know its changed.
    Im not sure the model way is the most compact way, but its how a model woul dinform the view
    data has changed so I think it should be ok.

    Do note its its what happens if you call item->setData on some role so
    that could also be used.

Log in to reply