Qt坐标系统

本文深入探讨Qt中坐标变换的概念,包括世界坐标、窗口视口坐标及设备坐标之间的转换,以及如何通过QPainter类实现这些变换。重点介绍了世界变换、窗口视口变换的实现方式和复合变换的应用,帮助开发者掌握Qt绘图中的坐标系统操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

坐标变换

World Corrdinates

==>

Window Coordinates

==>

Device Coordinates

(逻辑坐标)

世界变换

中间态坐标

窗口视口变换

(物理坐标)

在默认情况下,3个坐标系是一致的。

世界变换

世界变换直接相关的函数:

QPainter::setWorldMatrixEnabled

启用、禁用世界变换

QPainter::setWorldTransform

设置世界变换

QPainter::worldTransform

获取当前

QPainter::resetTransform

重置为QTransform()

4个常用的函数

QPainter::translate

平移

QPainter::scale

缩放

QPainter::rotate

旋转

QPainter::shear

剪切

注:它们通过直接调用的QTransform的相应成员直接修改世界变换

void QPainter::scale(qreal sx, qreal sy)
{
...
d->state->worldMatrix.scale(sx,sy);
...
}

世界变换的两个马甲:

QPainter::setTransform

QPainter::transform

void QPainter::setTransform(const QTransform &transform, bool combine )

{
setWorldTransform(transform, combine);

}

废弃的函数(Qt4.3开始,QTransform取代了QMatrix的位置,下列函数已不建议使用)

QPainter::setWorldMatrix

QPainter::worldMatrix

...

窗口视口变换

直接相关:

QPainter::setViewTransformEnabled

启用、禁用视口变换

QPainter::viewTransformEnabled

返回 视口变换的状态

QPainter::setViewport

设置视口(物理坐标)

QPainter::setWindow

设置窗口(与视口是同一矩形,中间态坐标)

该变换是简单的线性变换。

复合变换

窗口视口变换和世界变换的复合:

QPainter::combinedTransform

QTransform QPainter::combinedTransform() const

{
Q_D(const QPainter);
return d->state->worldMatrix * d->viewTransform();

}

典型应用:对鼠标事件的响应中,将坐标从物理坐标变换成QPainter需要的逻辑坐标

仿射变换、透射变换

Qt4.3(包括)之前的QMatrix只支持仿射变换(Affine transformation)

平移(Translation)

缩放(Scale

旋转(Rotation

剪切(Shear)

QTransform支持透射变换(perspective transformation)

m11

m12

m13

m21

m22

m23

m31
dx

m32
dy

m33

变换关系:

x' = m11*x + m21*y + dx
y' = m22*y + m12*x + dy
if (is not affine)

{
w' = m13*x + m23*y + m33
x' /= w'
y' /= w'

}

相关知识:

射影几何学仿射几何学微分几何学

参考

http://doc.qt.nokia.com/4.7/qtransform.html

http://doc.qt.nokia.com/4.7/qpainter.html

http://doc.qt.nokia.com/4.7/coordsys.html

本资源为Qt绘图基础,世界坐标系转换为逻辑坐标系。世界坐标系原点在视图左上角,本例子通过世界坐标转换,将坐标原点定位在视图中央,Y轴向上,X轴向右,并绘制坐标轴,基于逻辑坐标系下的绘图,可将转换关系函数取消生效,对比世界坐标系下的绘图。 重写PainterEvent函数: void QtPixPainter::paintEvent(QPaintEvent* event) { QPainter painter(this); // 反走样 painter.setRenderHint(QPainter::Antialiasing, true); //物理坐标系与逻辑坐标系的转换,如果不转换,下面的绘图都是在世界坐标系下 setWorldTransform(painter); // 其他一些绘制矩形,多边形的例子,经过上面转换,都是在逻辑坐标系下 drawRectScale(painter); //draw_shearRect(painter); //利用rotate()函数进行比例变换,实现缩放效果 //draw_rotate_act(painter); //draw_by_save_restore(painter); //transform_draw_SinX(painter); transform_draw(painter); local_drawConvexPolygon(painter); } // 将世界坐标(原点左上角)转换为逻辑坐标(原点在屏幕中间) QPointF QtPixPainter::mapToScene(const QPointF& point) { QTransform transMatrix = _transform.inverted(); //翻转矩阵? return transMatrix.map(point); //将点piont映射到transMatrix定义的坐标系中来 } // 将鼠标的逻辑位置返回并以标签形式展示 void QtPixPainter::mouseMoveEvent(QMouseEvent* event) { QString msg; QPointF mouse_po = mapToScene(event->pos()); //总是返回屏幕物理坐标系 double x = mouse_po.x(); // 总是返回屏幕物理坐标系 double y = mouse_po.y(); QString str = "(" + QString::number(x) + "," + QString::number(y) + ")"; //qDebug()<<"world x = "<pos().x()<<",world y = "<pos().y(); m_mouse_lable->setText(str); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值