本文介绍了QTableView派生类中的paintEvent:绘制设备返回引擎== 0,类型:1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 Qt 负载指示器的后续动画图像(又名预加载器)或替代方法? 我尝试在 QTableView 内绘画.但是当我初始化 QPainter 时,我收到以下警告.

As a follow up of Qt load indicator by animated image (aka preloader) or alternative? I try to paint inside a QTableView. But when I initialize the QPainter I get the following warnings.

QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1

这是代码(SO 答案,带有一个似乎可以工作的按钮):

Here is the code (SO answer, with a button it seems to work):

    void CDerivedFromQTableView::paintEvent(QPaintEvent *event)
    {
        QTableView::paintEvent(event); // draw original content
        QPainter p(this); // Problem: QPainter::begin: Paint device returned engine == 0, type: 1
        const QPixmap pm(QPixmap::grabWidget(this->m_loadIndicator));
        QPoint middle = this->geometry().center();
        int x = middle.x() - pm.width() / 2;
        int y = middle.y() - pm.height() / 2;
        p.drawPixmap(QPoint(x, y), pm); // draw load indicator inside QTableView
    }

我很惊讶创建 QPainter 失败,为什么会这样.我做错了什么?

I am surprised creating the QPainterfails, so why is that. What am I doing wrong?

简化版还是有警告

        QPainter p(this);
        QTableView::paintEvent(event);
        return;

当我注释掉 QPainter 时警告(当然)消失了,所以它似乎真的是根本原因,但为什么呢?

Warning (of course) gone when I comment out QPainter, so it really seems to be the root cause, but why?

推荐答案

由于 QTableViewQAbstractScrollArea 的子类,你应该打开 QPainter它的视口:

As QTableView is a subclass of QAbstractScrollArea you should open QPainter on its viewport:

void CDerivedFromQTableView::paintEvent(QPaintEvent *event)
{
    QTableView::paintEvent(event); // draw original content

    QPainter p(this->viewport());
    p.drawRect(0, 0, 20, 20);
}

文档说:

这个事件处理程序可以在子类中重新实现以接收绘画事件(在事件中传递),用于 viewport() 小部件.

注意:如果您打开一个画家,请确保在视口()上打开它.

Note: If you open a painter, make sure to open it on the viewport().

这篇关于QTableView派生类中的paintEvent:绘制设备返回引擎== 0,类型:1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 05:46