Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

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

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

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 
    }

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

The simplified version still gives the warning

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

The docs say it:

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...