问题描述
作为 Qt加载指示器的后续措施动画图像(又称预加载器)还是替代图像?我尝试在QTableView
内绘画.但是,当我初始化QPainter
时,会收到以下警告.
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
这是代码(SO答案,带有按钮的按钮似乎起作用):
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
}
我很惊讶创建QPainter
失败,为什么会这样.我在做什么错了?
I am surprised creating the QPainter
fails, so why is that. What am I doing wrong?
简化版仍会发出警告
QPainter p(this);
QTableView::paintEvent(event);
return;
当我注释掉QPainter
时,警告(当然)消失了,所以这似乎确实是根本原因,但是为什么?
Warning (of course) gone when I comment out QPainter
, so it really seems to be the root cause, but why?
推荐答案
由于QTableView
是QAbstractScrollArea
的子类,因此应在其视口中打开QPainter
:
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);
}
文档说:
注意:如果您打开一个画家,请确保在viewport()上将其打开.
Note: If you open a painter, make sure to open it on the viewport().
这篇关于QTableView派生类中的paintEvent:绘制设备返回的引擎== 0,类型:1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!