PDA

View Full Version : Customizing QTableView grid style



TKowal
7th June 2019, 13:00
Hello,

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))
{
painter->save();

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

painter->restore();
// 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:

13154

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:

13153

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())
{
update(visualRect(selected));
repaint(visualRect(selected));
}
}



(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.

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

Cheers,
_