Qt QTableView draw border around active cells

I am trying to implement a similar Excel behavior in a QTableView, where the border is painted around the entire current selection. I tried this, which seems like a hundred different ways and more and more problems arise. I can draw the border quite easily, but the remnants of the border are left whenever the choice changes. Here is one example I tried in QTableView :: paintEvent ...

void MyTableView::paintEvent(QPaintEvent* event) { // call QTableView paint event first so we can draw over it QTableView::paintEvent(event); // activeSelection is a list of indexes that is updated in another function // the function also calls QTableView::repaint whenever this list changes // in an attempt to erase the previously drawn border if(!activeSelection.size()) return; QRect rect = visualRect(activeSelection.at(0)) | visualRect(activeSelection.at(activeSelection.size() - 1)); // temporarily draw smaller border so it doesn't lie on the grid lines rect.adjust(4, 4, -4, -4); QPen pen(Qt::black, 2); QPainter painter(viewport()); painter.setPen(pen); painter.drawRect(rect); } 

This code produces results like this

I like any suggestions on how to make this run smoother. I tried to do this in the delegate, but then the delegate must know all the selected indexes and cannot draw the grid lines drawn by QTableView. Also, my table class needs to know where the border was drawn.

+6
source share
1 answer

try calling update (); in your selectionChanged function. this will slow down your implementation but remove the garbage.

+4
source

Source: https://habr.com/ru/post/891609/


All Articles