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

為{幸葍}努か 提交于 2020-01-30 13:14:08

问题


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?


回答1:


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



回答2:


I found a solution: By replacing

QPainter painter(this);

with

QPainter painter(viewport());

the problem was solved.



来源:https://stackoverflow.com/questions/52902375/qwidgetpaintengine-should-no-longer-be-called-in-a-qtreewidget-derived-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!