gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:57:01 27 4
gpt4 key购买 nike

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

Qt 版本:Qt 4.8 for embeddedQt 5.4 for DesktopQt 5.6 LTS for Desktop

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

enter image description here

我有一个从 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 示例,但我剥离了所有内容以突出问题。

  • 为什么会出现这个问题?
  • 为什么它只出现在一个轴上?

最佳答案

为了结束这个主题,我想添加对另一个 stackoverflow 问题的引用: Why does QPainter::drawPoint draw a horizontal line segment?

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

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

关于c++ - 为什么 QPainter 在一个轴上缩放(修饰)点,尽管它们不应该受到影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52181274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com