Qt QTableView 在活动单元格周围绘制边框

发布于 2024-11-17 13:03:17 字数 1147 浏览 3 评论 0原文

我正在尝试在 QTableView 中实现类似于 Excel 的行为,其中在整个当前选择周围绘制边框。我已经尝试了一百种不同的方法,但不断遇到问题。我可以很容易地绘制边框,但是每当选择发生变化时,就会留下边框的残余部分。这是我在 QTableView::paintEvent 中尝试过的一个示例...


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

该代码产生诸如 this

我希望得到任何有关如何使运行更顺利的建议。我曾尝试在委托中执行此操作,但是委托需要知道所选的所有索引,并且它无法绘制 QTableView 绘制的网格线。另外,我的表类需要知道边框在哪里绘制。

I'm trying to implement behavior similar Excel in a QTableView, where a border is painted around the entire current selection. I have tried this what feels like a hundred different ways and keep getting problems. I can draw the border easily enough, but remnants of the border are left whenever the selection changes. Here is one example I've tried in QTableView::paintEvent ...


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

That code produces results such as this

I would love any suggestions on how to make this run more smoothly. I had tried doing this in the delegate, but then the delegate needs to know all the indexes that are selected and it can't paint over the grid lines drawn by the QTableView. Plus, my table class needs to know where the border has been drawn.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-11-24 13:03:17

尝试调用 update();在您的选择更改功能中。这会减慢你的实施速度,但会消除垃圾。

try to call update(); in your selectionChanged function. this will slow out your implementation, but will remove garbage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文