2 Attachment(s)
Customizing QTableView grid style
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):
Code:
{
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>());
}
}
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:
Attachment 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:
Attachment 13153
Which is way closer to what I'm trying to achieve.
I've already tried to do this in my QTableView:
Code:
// 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.
Re: Customizing QTableView grid style
Hmm, maybe you need to call update() on the viewport() widget.
Cheers,
_