Search code examples
c++qtqt5qpainterqtreewidget

QWidget::paintEngine: Should no longer be called in a QTreeWidget derived class


I have a class, MyTree, which derived from QTreeWidget and

void MyTree::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
}

causes to raise the following issue,

QWidget::paintEngine: Should no longer be called

QPainter::begin: Paint device returned engine == 0, type: 1

Could someone please help me to solve the problem?


Solution

  • In the case of classes that inherit from QAbstractScrollArea as QTreeWidget and your MyTree the painting is not given directly in the widget but in the viewport() as indicated by the docs:

    void QAbstractScrollArea::paintEvent(QPaintEvent *event) Reimplemented from QFrame::paintEvent().

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

    So the solution is as follows:

    void MyTree::paintEvent(QPaintEvent *event)
    {
        QPainter painter(viewport());
    }