View Full Version : Customizing QTableView grid style

7th June 2019, 14:00

I'm wondering several things. I have subclassed QTableView to make a custom table. I'd like to be able to do several things.

First of all, I wanted the selected cells not to all have the "selected" color (blue by default) but instead have a frame around the selected cells (just like in Excel). To do this, I've used the following (in my custom QItemDelegate):

void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QModelIndex upIndex = index.sibling(index.row() - 1, index.column());
QModelIndex downIndex = index.sibling(index.row() + 1, index.column());
QModelIndex rightIndex = index.sibling(index.row(), index.column() + 1);
QModelIndex leftIndex = index.sibling(index.row(), index.column() - 1);

auto newOption = option;
if (option.state.testFlag(QStyle::State_Selected))

auto selIndexes = selM->selectedIndexes().toSet();
painter->setPen(QPen(Qt::red, 5));
if (!selIndexes.contains(rightIndex))
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
if (!selIndexes.contains(upIndex))
painter->drawLine(option.rect.topLeft(), option.rect.topRight());
if (!selIndexes.contains(downIndex))
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
if (!selIndexes.contains(leftIndex))
painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());

// newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight, index.data(Qt::BackgroundRole).value<QColor>());
newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight, Qt::gray);

This is probably not optimal, but I'd like to have something that works, before anything else.

Now it seems to be working, but it unfortunately isn't automatically repainted. What happens when I select cells is the following:


Which is not what I'm looking for at all. I guess it's not being repainted because (1) The points inside the zone are still red and (2) If I resize my window, I get the following:


Which is way closer to what I'm trying to achieve.

I've already tried to do this in my QTableView:

// selModel is my selection model
connect(selModel, &QItemSelectionModel::selectionChanged, [this]() {
for(const auto & selected : selModel->selectedIndexes())

(Before, I actually used setDirtyRegion but it didn't work either, so I figured I'd do something more... brutal.)

Please suggest if you have any ideas for any of the issues.

8th June 2019, 10:11
Hmm, maybe you need to call update() on the viewport() widget.