总结的结果是:
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. CentOS 7下安装RabbitMQ

    下载erlang:http://www.erlang.org/downloads ,otp_src_20.3.tar.gz 下载RabbitMQ: http://www.rabbitmq.com ,r ...

  2. 查看哪个用户、IP、什么时间登陆过服务器

    2019-01-07 utmpdump /var/log/wtmp 或者 who /var/log/wtmp

  3. Universal-Image-Loader完全解析(下)

    Universal-Image-Loader完全解析(下) 在这篇文章中,我会继续跟大家分享有关于Universal-Image-Loader框架的相关知识,这次主要分享的是框架中图片缓存方面的知识. ...

  4. (转)jieba中文分词的.NET版本:jieba.NET

    简介 平时经常用Python写些小程序.在做文本分析相关的事情时免不了进行中文分词,于是就遇到了用Python实现的结巴中文分词.jieba使用起来非常简单,同时分词的结果也令人印象深刻,有兴趣的可以 ...

  5. ubuntu设置root权限默认密码

    1.默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo passwd,然后输入当前用户的密码2.终端会提示我们输入新的密码并确认,此时的密码就是root新密 ...

  6. Object的原型拷贝-create、assign、getPrototypeOf 方法的结合

    一.实现原型拷贝 1.1.代码         tips:为了体现原型链,写了继承实现的代码,这部分可跳过- <script> /* 创建包含原型链的实验对象obj1-- start */ ...

  7. [Mysql 查询语句]——集合函数

    count() 统计记录条数 select count(*) from student; +----------+ | count(*) | +----------+ | +----------+ s ...

  8. [转]ASP.NET web API 2 OData enhancements

    本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...

  9. HtmlAnchor点击之后保持高亮

    HtmlAnchor点击之后保持高亮,就是一个链接,在点击之后,还要保持高亮状态.应用在网站后台管理界面,左边菜单点击之后,菜单保持点击高亮状态.为了实现这个功能,确实花上Insus.NET不少时间. ...

  10. Node.js创建第一个应用

    在我们创建 Node.js 第一个 "Hello, World!" 应用前,让我们先了解下 Node.js 应用是由哪几部分组成的: 引入 required 模块:我们可以使用 r ...