QTableView 派生的 paintEvent class: Paint device returned engine == 0, type: 1

paintEvent in QTableView derived class: Paint device returned engine == 0, type: 1

作为 的后续,我尝试在 QTableView 内部作画。但是当我初始化 QPainter 时,我收到以下警告。

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

这是代码(所以回答,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 失败,那是为什么。我做错了什么?

精简版还是有警告

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

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

由于 QTableViewQAbstractScrollArea 的子类,您应该在其视口上打开 QPainter

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

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

The docs说出来:

This event handler can be reimplemented in a subclass to receive paint events (passed in event), for the viewport() widget.

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