为什么 QPainter 在一个轴上缩放(修饰)点,尽管它们不应该受到影响?

Why does QPainter scale (cosmetic) points in one axis although they should not be affected?

我在我的 Qt 应用程序中绘制线条和点,但在 QPainter 中似乎存在缩放错误时遇到了问题。我的线条工作得很好,但点会受到某些比例转换的影响,尽管笔设置为 "cosmetic"。最让我困扰的是似乎只有一个轴(x 轴)受到影响。否则我可以将其归结为 transformation/matrix 代码中的一些浮点精度问题。

Qt 版本:Qt 4.8 嵌入式Qt 5.4 桌面Qt 5.6 LTS 桌面

这里有一些效果图,实际上看起来应该都一样(比如最左边的那个):

我有一个从 QWidget 派生的 RenderArea,它只是绘制上面的图像之一。 RenderArea 只是在 main() 中实例化并显示。那里的代码很简单,所以这里是相关的绘画代码:

void RenderArea::paintEvent(QPaintEvent* /* event */)
{
    const qreal scaleFactor = 0.01;

    // Define a "unit" square
    std::vector<QPointF> points;
    points.push_back(QPointF(0, 0));
    points.push_back(QPointF(1.0, 0));
    points.push_back(QPointF(1.0, 1.0));
    points.push_back(QPointF(0, 1.0));

    // Build a scaled version of the points
    for (unsigned i = 0; i < points.size(); i++) {
        points[i] *= scaleFactor;
    }

    QPainter painter(this);

    painter.save();

    // Scale the painter so that every square takes 100 pixels
    // in screen space regardless of the scaleFactor:
    painter.scale(100.0 / scaleFactor, 100.0 / scaleFactor);

    QPen pointPen(Qt::blue, 10);
    pointPen.setCosmetic(true);
    painter.setPen(pointPen);
    painter.drawPoints(points.data(), points.size());

    QPen linePen(Qt::red, 5);
    linePen.setCosmetic(true);
    painter.setPen(linePen);
    painter.drawPolyline(points.data(), points.size());

    painter.restore();
}

该示例基于 Qt 附带的 basicdrawing 示例,但我剥离了所有内容以突出问题。

为了结束这个主题,我想添加对另一个 Whosebug 问题的引用:

这似乎是 Qt 中的一个错误,已在此处提交: QPainter::drawPoints draws line segments instead of points

正如评论中所指出的,可以通过编写自己的绘制点代码、修补 Qt、绘制图像而不是点或使用 drawEllipse 来克服此错误。