QPainter drawLines方法

时间:2016-02-20 08:08:37

标签: c++ qt

我使用以下代码在qt中绘制图形的轴:

...

static const std::array<const QPointF,10*2> horizontalAxis = {
    QPointF(0.f,0.f), QPointF(1.f,0.f),
    // Horizontal scales
    QPointF(0.f,0.f), QPointF(0.f,1.f),
    QPointF(0.125f,0.f), QPointF(0.125f,1.f),
    QPointF(0.25f,0.f), QPointF(0.25f,1.f),
    QPointF(0.375f,0.f), QPointF(0.375f,1.f),
    QPointF(0.5f,0.f), QPointF(0.5f,1.f),
    QPointF(0.625f,0.f), QPointF(0.625f,1.f),
    QPointF(0.75f,0.f), QPointF(0.75f,1.f),
    QPointF(0.875f,0.f), QPointF(0.875f,1.f),
    QPointF(1.f,0.f), QPointF(1.f,1.f)
};

static const std::array<const QPointF,6*2> verticalAxis = {
    QPointF(0.f,0.f), QPointF(0.f,1.f),
    // Vertical scales
    QPointF(0.f,0.f), QPointF(-1.f,0.f),
    QPointF(0.f,0.25f), QPointF(-1.f,0.25f),
    QPointF(0.f,0.5f), QPointF(-1.f,0.5f),
    QPointF(0.f,0.75f), QPointF(-1.f,0.75f),
    QPointF(0.f,1.f), QPointF(-1.f,1.f)
};

...

void Plot::paint(QPainter& p)
{
    int const width = p.device()->width();
    int const height = p.device()->height();
    Q_ASSERT(width > xpaddingLeft + xpaddingRight);
    Q_ASSERT(height > ypaddingTop + ypaddingBottom);

    if(dirty) {
        recalculate();
    }

    p.save();

    int const bottomLeftX = xpaddingLeft;
    int const bottomLeftY = height - ypaddingBottom;
    int const scaleX = width - xpaddingLeft - xpaddingRight;
    int const scaleY = height - ypaddingTop - ypaddingBottom;

    p.setRenderHint(QPainter::HighQualityAntialiasing,false);
    p.setRenderHint(QPainter::Antialiasing,false);

    QPen pen(QBrush(QColor::fromRgb(0,0,0)), 2);
    pen.setCosmetic(true);  // Do not apply scale to the pen
    p.setPen(pen);

    // Horizontal Axis
    p.resetTransform();
    p.translate(bottomLeftX,bottomLeftY);   // Origin at bottom left
    p.scale(scaleX,scaleSize); // y-axis points down. Only scale y to match the scale-mark size
    p.drawLines(&horizontalAxis[0], (int)horizontalAxis.size());

    // Vertical Axis
    p.resetTransform();
    p.translate(bottomLeftX,bottomLeftY);
    p.scale(scaleSize,-scaleY); // y-axis points up. Only scale x to match the scale-mark size
    p.drawLines(&verticalAxis[0], (int)verticalAxis.size());

    ...
}

这会产生如下图形,对lhs有一个奇怪的伪像(仅显示没有加载数据的轴):
水平轴刻度会导致LHS的奇怪伪影 Horizontal axis scales result in weird artifact to the LHS

注释掉水平刻度定义的所有QPointF定义会删除这个奇怪的工件。然而,评论任何一对点并不会影响它。如果我从使用drawLines()方法切换,如下所示,拉伸消失。

    //p.drawLines(&horizontalAxis[0], (int)horizontalAxis.size());
    for(size_t k = 0; k < horizontalAxis.size()-1; k += 2) p.drawLine(horizontalAxis[k],horizontalAxis[k+1]);

我得到的结果如下,没有任何文物(只显示没有加载数据的轴):
水平轴刻度显示无切换 Horizontal axes scales show no switching

我错过了什么或者这是drawLines()中的错误吗?

我使用drawLines()函数两次绘制垂直轴和实际数据(作为折线图)没有(明显)问题,但我想更确定它不会导致奇数我无法设法测试的情况下的文物。

1 个答案:

答案 0 :(得分:1)

您使用的drawLines()重载(来自Qt5文档):

QPainter::drawLines (const QPointF* pointPairs, int lineCount)

我可以提请您注意第二个参数。它被称为lineCount,而不是点数,所以你必须传递HALF的点数:

assert (0 == horizontalAxis.size () % 2);
p.drawLines (&horizontalAxis[0], static_cast<int> (horizontalAxis.size () / 2));