現在の選択範囲全体の周囲に境界線が描画されるQTableViewにExcelと同様の動作を実装しようとしています。私はこれを100通りの方法で試し、問題を抱え続けています。簡単に境界線を描くことができますが、選択が変わるたびに境界線の残骸が残ります。これが私がQTableView::paintEvent..で試した1つの例です。
void MyTableView::paintEvent(QPaintEvent* event)
{
// call QTableView's 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);
}
そのコードは次のような結果を生成します
これをよりスムーズに実行する方法についての提案があればいいのですが。デリゲートでこれを実行しようとしましたが、デリゲートは選択されているすべてのインデックスを知る必要があり、QTableViewによって描画されたグリッド線上にペイントできません。さらに、私のテーブルクラスは、境界線が描画された場所を知る必要があります。