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):

Qt Code:
  1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3. QModelIndex upIndex = index.sibling(index.row() - 1, index.column());
  4. QModelIndex downIndex = index.sibling(index.row() + 1, index.column());
  5. QModelIndex rightIndex = index.sibling(index.row(), index.column() + 1);
  6. QModelIndex leftIndex = index.sibling(index.row(), index.column() - 1);
  7.  
  8. auto newOption = option;
  9. if (option.state.testFlag(QStyle::State_Selected))
  10. {
  11. painter->save();
  12.  
  13. auto selIndexes = selM->selectedIndexes().toSet();
  14. painter->setPen(QPen(Qt::red, 5));
  15. if (!selIndexes.contains(rightIndex))
  16. painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
  17. if (!selIndexes.contains(upIndex))
  18. painter->drawLine(option.rect.topLeft(), option.rect.topRight());
  19. if (!selIndexes.contains(downIndex))
  20. painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
  21. if (!selIndexes.contains(leftIndex))
  22. painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
  23.  
  24. painter->restore();
  25. // newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight, index.data(Qt::BackgroundRole).value<QColor>());
  26. newOption.palette.setBrush(QPalette::Normal, QPalette::Highlight, Qt::gray);
  27. }
  28. }
To copy to clipboard, switch view to plain text mode 

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:

Current.png

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:

KindaCorrect.PNG

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

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

Qt Code:
  1. // selModel is my selection model
  2. connect(selModel, &QItemSelectionModel::selectionChanged, [this]() {
  3. for(const auto & selected : selModel->selectedIndexes())
  4. {
  5. update(visualRect(selected));
  6. repaint(visualRect(selected));
  7. }
  8. }
To copy to clipboard, switch view to plain text mode 


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