问题
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