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

总结一:经过试验,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());

代码如下,跟踪任何控件,记得设置跟踪轨迹为真setMouseTracking(true)

void TestWidget::mouseMoveEvent(QMouseEvent* event)
{  QPoint m = event->globalPos();  ui.lblMouseEventGlobalPos->setText(QString("(%1,%2)").arg(m.x()).arg(m.y()));  QPoint n = QCursor::pos();  ui.lblCursorPos->setText(QString("(%1,%2)").arg(n.x()).arg(n.y()));  QPoint k = ui.posBtn->pos();  QPoint h = ui.posBtn->mapToGlobal(ui.posBtn->pos());  QPoint i = ui.posBtn->mapToGlobal(ui.posBtn->pos());  ui.lblPushBttonPos->setText(QString("(%1,%2)").arg(k.x()).arg(k.y()));  ui.lblToParentPos->setText(QString("(%1,%2)").arg(h.x()).arg(h.y()));  ui.lblToGlobalPos->setText(QString("(%1,%2)").arg(i.x()).arg(i.y()));  QRect widgetRect = ui.posBtn->geometry();  QPoint mousePos = ui.posBtn->mapFromGlobal(QCursor::pos());  if (widgetRect.contains(mousePos))  {  ui.posBtn->setText("摸到我了");  }  else  {  ui.posBtn->setText("....");  }  }  

Qt全局坐标和相对坐标相关推荐

  1. qt 旋转后的三维坐标_OpenGL + Qt: 3 - 旋转动画和键盘操纵

    前三篇链接: 这一周笔者经历了漫长的洲际飞行和昏天黑地的倒时差,所以本篇内容相对少一些,侧重 Qt 而不是 OpenGL.在上一篇中,我们绘制了一个正四面体,然而正四面体的一个特点是无论你从哪个角度看 ...

  2. Qt中视图 场景 图元坐标的转换

    场景坐标 场景坐标是所有图元的基础坐标系统.场景坐标系统描述了顶层的图元,每个图元都有场景坐标和相应的包容框.场景坐标的原点在场景中心,坐标原点是X轴正方向向右,Y轴正方向向下. QGraphicsS ...

  3. Qt:相对坐标转绝对坐标

    //相对坐标转绝对坐标 QPoint QWidget::mapToGlobal(const QPoint &pos) constmove(mapToGlobal(_pushButton-> ...

  4. 从像素坐标到相机坐标_多视图几何基础——深入理解相机内外参数

    上一篇:前言(comming soon) 关键词:相机模型,多视图几何,相机内参数,相机外参数,skew畸变 1. 针孔相机模型 Figure 1 针孔相机模型是一种理想化的简单相机模型,也是成像的最 ...

  5. 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系互转

    为什么写这个模块 随着移动互联网的兴起,几乎每一个app都会去收集用户位置,如果恰好你在处理与地理定位相关的代码,并且不了解地理坐标系的话,肯定要被我大天朝各种坐标系搞晕.写这个模块的目的也是因为项目 ...

  6. 火星坐标、百度坐标、WGS84坐标转换代码(JS、python版)

    火星坐标.百度坐标.WGS84坐标转换代码(JS.python版) 一.JS版本源码 github:https://github.com/wandergis/coordTransform /*** C ...

  7. 一种近似方法将场地坐标转为像素坐标

    一种近似方法将场地坐标转为像素坐标 先上代码: for (int i = 0;i < WIDTH;i++){for (int j = 0;j < HEIGHT;j++){nNewi = i ...

  8. 火星坐标、百度坐标、WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版...

    转自:https://www.cnblogs.com/fwc1994/p/5884115.html 火星坐标.百度坐标.WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版 火星坐标 ...

  9. 【SeeMusic】视频编辑 ( 视频 X 坐标 | 视频 Y 坐标 | 视频旋转 | 视频扭曲 )

    SeeMusic 系列文章目录 [SeeMusic]下载安装并注册 SeeMusic 软件 [SeeMusic]创建 SeeMusic 工程并编辑相关内容 ( 创建工程 | 导入 MIDI 文件 | ...

  10. matplotlib 多子图的画法 - 设置坐标范围 - 设置坐标的显示间隔 - 设置figure的大标题 - 设置x轴和y轴的名称 - df.groupby

    前言 本文实现的功能: 多子图的画法 设置坐标范围 设置坐标的显示间隔 设置figure的大标题 设置x轴和y轴的名称

最新文章

  1. php中的unbuffered_row,php – 加载数据infile和unbuffered查询错误
  2. 将表单中查询参数转换为json
  3. [js] 在DOM上同时绑定两个点击事件(一个用捕获,一个用冒泡),事件总共会执行几次,先执行哪个事件?
  4. shell中的大括号和小括号
  5. 推荐腾讯最新重磅开源项目!
  6. Windows7 环境下 VS2008 C++链接错误!
  7. mysql自动判断索引机制_Mysql优化之索引实现原理
  8. 自我理解:封装、继承和多态
  9. 正则表达式之断言及常用正则表达式
  10. iOS开发常用三方库、插件、知名博客等等
  11. VCL界面控件DevExpress VCL发布v18.1.7|附下载
  12. redis探索之常用的三种缓存读写策略
  13. python量化交易--因子选股策略
  14. 如何安排自己大学阶段的学习才能成为一名优秀的 Quant?
  15. SpringCloud 微服务架构开源项目,适合接私活、毕业设计(附源码)
  16. 如何正确使用关键路径图?
  17. 【C语言】案例四十六 点名册(一)
  18. veins中实现rsu与车辆通信
  19. 火焰焰心matlab,火焰心_刘德华_高音质在线试听_火焰心歌词|歌曲下载_酷狗音乐...
  20. 混沌映射singer map 和 logistic map分叉图

热门文章

  1. 生成xslx文件,写入并读取
  2. 美国圣诞8日西海岸自驾游
  3. [oeasy]python0125_汉字打印机_点阵式打字机_汉字字形码
  4. Re:从零开始的DS学习 十大排序算法我都整理好了
  5. 调用微信二维码识别开源库
  6. 防弹玻璃为啥会被钢球砸碎?这就是一道高中物理题!
  7. ERR_NETWORK_CHANGED
  8. 飞腾CPU体系结构(九)
  9. 如果让你设计一个微信朋友圈,你怎么设计
  10. 记忆测试系统java代码_JAVA课程设计——记忆测试系统(附源程序).doc