总结的结果是:
QMouseEvent中两类坐标系统,一类是窗口坐标,一类是显示器坐标。
 
总结一:经过试验,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("....");
} }
==========================基础知识=================

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. 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. 

Qt获取控件位置,坐标总结的更多相关文章

  1. js获取控件位置以及不同浏览器中的差别

    js获取控件位置(坐标位置)在不同浏览器中的差别. //获取坐标位置 function getpos(e) { var t=e.offsetTop; var l=e.offsetLeft; var h ...

  2. js获取控件位置

    //获取坐标位置 function getpos(e) { var t=e.offsetTop; var l=e.offsetLeft; var height=e.offsetHeight; whil ...

  3. swift 获取控件位置 大小

    var SearchBtn = uibutton() SearchBtn.frame.origin.x   //获取坐标x SearchBtn.frame.origin.Y  // 获取坐标Y Sea ...

  4. javascript控制滚动条的位置,获取控件的位置

    一.如下是定位鼠标在视窗中的位置,先定位视窗和页面直接的距离. function getMousePoint() { var point = {x:0,y:0}; // 如果浏览器支持 pageYOf ...

  5. android 获取屏幕宽高 和 获取控件坐标

    一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width ...

  6. Appium+python自动化(二十五)- 那些让人抓耳挠腮、揪头发和掉头发的事 - 获取控件ID(超详解)

    简介 在前边的第二十二篇文章里,已经分享了通过获取控件的坐标点来获取点击事件的所需要的点击位置,那么还有没有其他方法来获取控件点击事件所需要的点击位置呢?答案是:Yes!因为在不同的大小屏幕的手机上获 ...

  7. jQuery学习笔记(控件位置定位、尺寸大小的获取等)

    想做一个幽灵按钮出来,效果大概如下图: 当点击按钮的时候,会有四根线条从四个方向飞入,经历从“无-有-无”的闪入过程. 那么我的设计想法是,先在HTML中定义一个按钮,然后在jQuery中设计按钮点击 ...

  8. [WPF]获取控件间的相对位置

    原文:[WPF]获取控件间的相对位置 [WPF]获取控件间的相对位置                             周银辉 我们知道WPF有着比较灵活的布局方式,关于某个控件的坐标,Canv ...

  9. mfc获取控件在对话框上的位置

    CRect rect; GetDlgItem(控件ID)->GetWindowRect(&rect);//获取控件的屏幕坐标ScreenToClient(&rect);//转换为 ...

随机推荐

  1. [ZJOI2019]语言[树链的并、线段树合并]

    题意 题目链接 分析 考虑枚举每个点的答案,最后除以 2 即可. 可以与 \(u\) 构成合法点对 的集合 为所有经过了 \(u\) 的链的并.因为这些链两两有交,根据结论 "树上两条相交的 ...

  2. 论文分享NO.1(by_xiaojian)

    论文分享第一期-2019.03.14: 1. Non-local Neural Networks  2018 CVPR的论文 2. Self-Attention Generative Adversar ...

  3. ECharts概念学习系列之ECharts官网教程之在 webpack 中使用 ECharts(图文详解)

    不多说,直接上干货! 官网 http://echarts.baidu.com/tutorial.html#%E5%9C%A8%20webpack%20%E4%B8%AD%E4%BD%BF%E7%94% ...

  4. reifiable type与raw type

    下面的逻辑需要明白如下两个概念: 4.7. Reifiable Types 4.8. Raw Types 举几个是Reifiable Types的例子,如下: class A{} class B< ...

  5. 【Qt开发】QDate类

    QDate为开发者提供日期的类,函数也很丰富 常用方法介绍 1.QDate addDays(qint64 ndays) const 当前日期添加n天,n可以为负 2.QDate addMonths(i ...

  6. 基础js--调试js

    1,逻辑错误 常见错误: 是否由于拼写错误而导致申明了新的变量? 是否在条件判定上出现了疏漏? 方法:使用开发者工具调试代码 2,代码错误 常见错误: 是否拼写错误? 是否使用中文的符号? 扩展: 1 ...

  7. SQL语句映射文件(1)resultMap

    SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用,例如: ...

  8. ES6 读书笔记

    一.let和const命令 二.变量的解构赋值 三.字符串的扩展 四.数值的扩展 五.正则的扩展 六.数组的扩展 七.函数的扩展 八.对象的扩展 九.symbol 十.proxy和reflect 十一 ...

  9. 吴恩达《深度学习》第五门课(1)循环序列模型(RNN)

    1.1为什么选择序列模型 (1)序列模型广泛应用于语音识别,音乐生成,情感分析,DNA序列分析,机器翻译,视频行为识别,命名实体识别等众多领域. (2)上面那些问题可以看成使用(x,y)作为训练集的监 ...

  10. IIS 站点 共享目录

    1.先建立站点,再设置文件夹为共享,Everyone 2.Mac电脑 Everyone不能访问,必须建立用户