【问题标题】:How do I set up a QPainter window when logical coordinates are small and fractional?当逻辑坐标很小且小数时,如何设置 QPainter 窗口?
【发布时间】:2017-03-10 10:10:50
【问题描述】:

我想在 QWidget 上渲染一个场景,我需要定义一个从世界坐标到屏幕坐标的转换。 QPainter::setWindow 似乎是正确使用的函数,但它将其逻辑坐标定义为 int,而不是 float

我的逻辑坐标不仅是浮点数,它们也非常小。逻辑坐标的窗口类似于(1.5,1.5)..(1.54,1.53)

我不能使用setWindow,因为路由错误完全破坏了我的坐标,所以我如何设置QPainter 转换,以便在矩形具有任意值时渲染矩形(x,y)..(x+width,y+height) 将完全填充小部件浮点值?

(已选择矩形以具有正确的纵横比以适合小部件。)

【问题讨论】:

  • 有什么理由可以执行另一个简单的转换吗?也就是说,与其将逻辑坐标指定为 1.54 个“事物”,不如将其指定为 1540 毫“事物”?
  • a) 小部件用于放大,b) 转换通常不是基于整数的

标签: qt coordinates coordinate-transformation


【解决方案1】:

您可能会编写自己的“浮点”版本的QPainter::setWindow...

void set_painter_window (QPainter &painter, const QRectF &logical_rect)
{
  QTransform xform;
  xform.scale(painter.viewport().width() / logical_rect.width(),
              painter.viewport().height() / logical_rect.height());
  xform.translate(-logical_rect.left(), -logical_rect.top());
  painter.setTransform(xform);
}

在我拼凑起来的几个简单测试中似乎可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2010-10-11
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多