在paintEvent中警告QPainter [英] Warning QPainter inside paintEvent

查看:778
本文介绍了在paintEvent中警告QPainter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在QChartView中绘制更多信息,所以我重新实现paintEvent

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

我正在处理这些错误

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

对此有何想法?我也用过painter.begin和end,但是我犯了同样的错误

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

推荐答案

请勿在 QChartView 的子类上绘制,而应在其视口上绘制.

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

QChartView 源自此答案(以及如文档所述),您应该将视口用作绘画您的 QPainter 设备,而不是小部件本身.

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.

所以,而不是

QPainter painter(this);

QPainter painter(viewport());

QGraphicsView的来源,即:

// Set up the painter
QPainter painter(viewport());

这篇关于在paintEvent中警告QPainter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆