Qt的一些坐标相关的问题
2017-06-21 11:11
323 查看
QMouseEvent中两类坐标系统,一类是窗口坐标,一类是显示器坐标。
1、QPoint QMouseEvent::pos()
这个只是返回相对这个widget(重载了QMouseEvent的widget)的位置。
const Returns the position of the mouse cursor, relative to the widget that received the event. If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.
2、QPoint QMouseEvent::globalPos()
窗口坐标,这个是返回鼠标的全局坐标
const Returns the global position of the mouse cursor at the time of the event. This is important on asynchronous window systems like X11. Whenever you move your widgets around in response to mouse events, globalPos() may differ a lot from the current
pointer position QCursor::pos(), and from QWidget::mapToGlobal(pos()).
3、QPoint QCursor::pos() [static]
返回相对显示器的全局坐标
Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates. You can call QWidget::mapFromGlobal() to translate it to widget coordinates. Note: The position is queried from the windowing system. If mouse events
are generated via other means (e.g., via QWindowSystemInterface in a unit test), those fake mouse moves will not be reflected in the returned value. Note: On platforms where there is no windowing system or cursors are not available, the returned position is
based on the mouse move events generated via QWindowSystemInterface.
4.1 QPoint QWidget::mapToGlobal(const QPoint & pos) const
将窗口坐标转换成显示器坐标
Translates the widget coordinate pos to global screen coordinates. For example, mapToGlobal(QPoint(0,0)) would give the global coordinates of the top-left pixel of the widget. See also mapFromGlobal(), mapTo(), and mapToParent().
4.2 QPoint QWidget::mapFromGlobal(const QPoint & pos) const
将显示器坐标转换成窗口坐标
Translates the global screen coordinate pos to widget coordinates.
5.1 QPoint QWidget::mapToParent(const QPoint & pos) const
将窗口坐标获得的pos转换成父类widget的坐标
Translates the widget coordinate pos to a coordinate in the parent widget.
5.2 QPoint QWidget::mapFromParent(const QPoint & pos) const
将父类窗口坐标转换成当前窗口坐标
Translates the parent widget coordinate pos to widget coordinates. Same as mapFromGlobal() if the widget has no parent.
6.3 QPoint QWidget::mapTo(const QWidget * parent, const QPoint & pos) const
将当前窗口坐标转换成指定parent坐标。
Translates the widget coordinate pos to the coordinate system of parent. The parent must not be 0 and must be a parent of the calling widget. See also mapFrom(), mapToParent(), mapToGlobal(), and underMouse().
7、QWidget::pos() : QPoint
这个属性获得的是当前目前控件在父窗口中的位置,
This property holds the position of the widget within its parent widget.
If the widget is a window, the position is that of the widget on the desktop, including its frame.
When changing the position, the widget, if visible, receives a move event (moveEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown.
By default, this property contains a position that refers to the origin.
Warning: Calling move() or setGeometry() inside moveEvent() can lead to infinite recursion.
See the Window Geometry documentation for an overview of geometry issues with windows.
8、const QPointF & QMouseEvent::screenPos() const
Returns the position of the mouse cursor as a QPointF, relative to the screen that received the event.
和QPoint QMouseEvent::globalPos() 值相同,但是类型更高精度的QPointF
This function was introduced in Qt 5.0.
总结一:经过试验,QMouseEvent::globalPos() 和 QCursor::pos()效果一样,但是Qt帮助文档说不一样,可是我获得值确实相同的。
QCursor::pos() == QMouseEvent::globalPos() 都是全局坐标;
总结二:将button:posBtn直接转换成全局坐标。
QMouseEvent::globalPos() == ui.posBtn->mapToGlobal(ui.posBtn->pos());
总结三:将全局坐标(鼠标当前坐标,QCursor::pos())直接转换成当前
当前窗口相对坐标 == ui.posBtn->mapFromGlobal(QCursor::pos());
如下图:
上面的mouseEvent.globalPos()和QCursor::pos()永远相同,都是全局坐标。
上面绿色按钮的当前坐标:ui.pushButton->pos() 、转换父窗口坐标后mapToParent()、转换成全局坐标后mapToGlobal();
如果当前鼠标坐标摸到按钮,按钮上面的文字发生变化,经过比较。
确实得到:QCursor::pos() == ui.posBtn->mapFromGlobal(QCursor::pos());
1、QPoint QMouseEvent::pos()
这个只是返回相对这个widget(重载了QMouseEvent的widget)的位置。
const Returns the position of the mouse cursor, relative to the widget that received the event. If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.
2、QPoint QMouseEvent::globalPos()
窗口坐标,这个是返回鼠标的全局坐标
const Returns the global position of the mouse cursor at the time of the event. This is important on asynchronous window systems like X11. Whenever you move your widgets around in response to mouse events, globalPos() may differ a lot from the current
pointer position QCursor::pos(), and from QWidget::mapToGlobal(pos()).
3、QPoint QCursor::pos() [static]
返回相对显示器的全局坐标
Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates. You can call QWidget::mapFromGlobal() to translate it to widget coordinates. Note: The position is queried from the windowing system. If mouse events
are generated via other means (e.g., via QWindowSystemInterface in a unit test), those fake mouse moves will not be reflected in the returned value. Note: On platforms where there is no windowing system or cursors are not available, the returned position is
based on the mouse move events generated via QWindowSystemInterface.
4.1 QPoint QWidget::mapToGlobal(const QPoint & pos) const
将窗口坐标转换成显示器坐标
Translates the widget coordinate pos to global screen coordinates. For example, mapToGlobal(QPoint(0,0)) would give the global coordinates of the top-left pixel of the widget. See also mapFromGlobal(), mapTo(), and mapToParent().
4.2 QPoint QWidget::mapFromGlobal(const QPoint & pos) const
将显示器坐标转换成窗口坐标
Translates the global screen coordinate pos to widget coordinates.
5.1 QPoint QWidget::mapToParent(const QPoint & pos) const
将窗口坐标获得的pos转换成父类widget的坐标
Translates the widget coordinate pos to a coordinate in the parent widget.
5.2 QPoint QWidget::mapFromParent(const QPoint & pos) const
将父类窗口坐标转换成当前窗口坐标
Translates the parent widget coordinate pos to widget coordinates. Same as mapFromGlobal() if the widget has no parent.
6.3 QPoint QWidget::mapTo(const QWidget * parent, const QPoint & pos) const
将当前窗口坐标转换成指定parent坐标。
Translates the widget coordinate pos to the coordinate system of parent. The parent must not be 0 and must be a parent of the calling widget. See also mapFrom(), mapToParent(), mapToGlobal(), and underMouse().
7、QWidget::pos() : QPoint
这个属性获得的是当前目前控件在父窗口中的位置,
This property holds the position of the widget within its parent widget.
If the widget is a window, the position is that of the widget on the desktop, including its frame.
When changing the position, the widget, if visible, receives a move event (moveEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown.
By default, this property contains a position that refers to the origin.
Warning: Calling move() or setGeometry() inside moveEvent() can lead to infinite recursion.
See the Window Geometry documentation for an overview of geometry issues with windows.
8、const QPointF & QMouseEvent::screenPos() const
Returns the position of the mouse cursor as a QPointF, relative to the screen that received the event.
和QPoint QMouseEvent::globalPos() 值相同,但是类型更高精度的QPointF
This function was introduced in Qt 5.0.
总结一:经过试验,QMouseEvent::globalPos() 和 QCursor::pos()效果一样,但是Qt帮助文档说不一样,可是我获得值确实相同的。
QCursor::pos() == QMouseEvent::globalPos() 都是全局坐标;
总结二:将button:posBtn直接转换成全局坐标。
QMouseEvent::globalPos() == ui.posBtn->mapToGlobal(ui.posBtn->pos());
总结三:将全局坐标(鼠标当前坐标,QCursor::pos())直接转换成当前
当前窗口相对坐标 == ui.posBtn->mapFromGlobal(QCursor::pos());
如下图:
上面的mouseEvent.globalPos()和QCursor::pos()永远相同,都是全局坐标。
上面绿色按钮的当前坐标:ui.pushButton->pos() 、转换父窗口坐标后mapToParent()、转换成全局坐标后mapToGlobal();
如果当前鼠标坐标摸到按钮,按钮上面的文字发生变化,经过比较。
确实得到:QCursor::pos() == ui.posBtn->mapFromGlobal(QCursor::pos());
相关文章推荐
- jpct-ae的相对坐标与世界坐标及一些相关的小问题
- 读本期《程序员》(8月刊)而引发的对计算机相关专业毕业生就业问题的一些思考
- QT的中文化相关问题
- QT开发之deigner+汉字显示等相关问题
- 一些系统相关问题解决方案
- Linux下 QT的一些问题集锦
- QT安装及编程的一些问题
- VS2010中集成 QT 遇到的一些问题
- 与j2me签名相关的需要注意的一些问题
- Axure RP Pro - 相关问题 - 一些字符无法直接进入编辑部件文字状态
- webkit(qt)的一些问题
- [原创]解决virtualbox及一些qt应用程序字体难看的问题
- CDialog中不响应OnKeyDown和一些相关问题
- 有关 远程桌面 的一些相关问题
- QT的一些问题
- Google Earth的一些相关坐标
- 企业对于搜索引擎优化的一些常见问题以及相关的解答
- 系统分析,业务建模,UML,RUP相关的一些问题和回复
- 说说我对iAD的错误理解及一些相关问题
- 与cache相关的一些问题和思考