• 13597阅读
  • 5回复

【提问】怎样把屏幕坐标转换成逻辑坐标 [复制链接]

上一主题 下一主题
离线cocalele
 
只看楼主 倒序阅读 楼主  发表于: 2006-02-27
在绘图时使用了painter.setWindow(0,0,50,50)
然后在鼠标点击事件里面得到的坐标是屏幕坐标,但是我希望能得到逻辑坐标。在Windows里有一个函数DPtoLP可以进行这样的转换,在Qt里没有找到,有谁知道的吗?谢谢大家!
离线XChinux

只看该作者 1楼 发表于: 2006-02-27
QMouseEvent里有,分别是int golbalX(), int globalY(), int x(), int y()
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线cocalele
只看该作者 2楼 发表于: 2006-02-28
下面是引用XChinux于2006-02-27 14:05发表的:
QMouseEvent里有,分别是int golbalX(), int globalY(), int x(), int y()


我看帮助,感觉这个是屏幕坐标和相对于窗口原点的坐标吧,用了setWindows后不仅仅是原点发生了变化,比例也发生变化了,视图进行了缩放。
离线zccmin
只看该作者 3楼 发表于: 2006-02-28
请问一下,什么叫逻辑坐标呢?
离线cocalele
只看该作者 4楼 发表于: 2006-03-01
下面是引用zccmin于2006-02-28 11:28发表的:
请问一下,什么叫逻辑坐标呢?


比如在MVC模型里面,Model和View是分开的,Model里面的数据要表现在View上。两个的坐标系统可以不一样,Model用的就可以认为是逻辑坐标,View使用的则可以认为是设备坐标(或者屏幕坐标)
离线nanhu_007

只看该作者 5楼 发表于: 2006-03-02
Let's take a closer look at the coordinate system defined by the viewport, window, and world matrix. (In this context, the term "window" does not refer to a window in the sense of a top-level widget, and the "viewport" has nothing to do with QScrollView's viewport.)

The viewport and the window are tightly bound. The viewport is an arbitrary rectangle specified in physical coordinates. The window specifies the same rectangle, but in logical coordinates. When we do the painting, we specify points in logical coordinates, and those coordinates are converted into physical coordinates in a linear algebraic manner, based on the current window–viewport settings.

By default, the viewport and the window are set to the device's rectangle. For example, if the device is a 320 x 200 widget, both the viewport and the window are the same 320 x 200 rectangle with its top-left corner at position (0, 0). In this case, the logical and physical coordinate systems are the same.

The window–viewport mechanism is useful to make the drawing code independent of the size or resolution of the paint device. We can always do the arithmetic to map logical coordinates to physical coordinates ourselves, but it's usually simpler to let QPainter do the work. For example, if we want the logical coordinates to extend from (-50, -50) to (+50, +50), with (0, 0) in the middle, we can set the window as follows:

painter.setWindow(QRect(-50, -50, 100, 100));

The (-50, -50) pair specifies the origin, and the (100, 100) pair specifies the width and height. This means that the logical coordinates (-50, -50) now correspond to the physical coordinates (0, 0), and the logical coordinates (+50, +50) correspond to the physical coordinates (320, 200). In this example, as is often the case, we don't need to change the viewport.
//////////////////////////////////////
请参考Prentice Hall - C++ GUI Programming with Qt 3.chm
快速回复
限100 字节
 
上一个 下一个