当逻辑坐标很小并且是小数时,如何设置 QPainter 窗口?

spr*_*aff 4 qt coordinates coordinate-transformation

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

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

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

(矩形已被选择为具有正确的长宽比以适合小部件。)

G.M*_*.M. 5

您可能可以编写自己的“浮点”版本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);
}
Run Code Online (Sandbox Code Playgroud)

在我拼凑起来的几个简单测试中,似乎按预期工作。