我是靠谱客的博主 阳光微笑,最近开发中收集的这篇文章主要介绍paintevent占cpu,在paintEvent中警告QPainter,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I tried to draw some more information in QChartView so I re-implement paintEvent

virtual void paintEvent(QPaintEvent *event) {

QChartView::paintEvent(event);

OmenChart *mchr = dynamic_cast(this->chart());

if(mchr == nullptr)

return;

QPainter painter(this);

const int labelOffset = 2 + 2;

painter.setFont(this->font());

painter.setPen(QPen(Qt::black));

QFontMetrics fm(painter.font());

const OmenScatterSeries *omnSr = mchr->serie();

QVector points = omnSr->pointsVector();

QStringList labels = omnSr->pointLabels();

for (int i(0); i < labels.count(); i++) {

QString pointLabel = labels[i];

// Position text in relation to the point

int pointLabelWidth = fm.width(pointLabel);

QPointF position(points.at(i));

position.setX(position.x() - pointLabelWidth / 2);

position.setY(position.y() - labelOffset);

painter.drawText(position, pointLabel);

}

}

and I am taking these errors

QWidget::paintEngine: Should no longer be called

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

QPainter::setFont: Painter not active

QPainter::setPen: Painter not active

QPainter::font: Painter not active

QWidget::paintEngine: Should no longer be called

Any ideas on this ? I had also used painter.begin and end but I took the same errors

解决方案

Do not draw on your subclass of QChartView, but instead on its viewport.

QChartView is derived from QGraphicsView, which in turn is derived from QAbstractScrollArea and according to this answer (as well as the cited there documentation) you should use the viewport as a paint device for your QPainter, not the widget itself.

So, instead of

QPainter painter(this);

write

QPainter painter(viewport());

The same could be seen in the source of QGraphicsView, i.e.:

// Set up the painter

QPainter painter(viewport());

最后

以上就是阳光微笑为你收集整理的paintevent占cpu,在paintEvent中警告QPainter的全部内容,希望文章能够帮你解决paintevent占cpu,在paintEvent中警告QPainter所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部