Qt全局坐标和相对坐标



  • 首先:

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());


转载自:http://jingyan.baidu.com/article/48206aeaffc02c216ad6b31d.html

<think>好的,我现在需要回答用户关于如何在Qt中获取控件全局坐标位置的问题。首先,我得回忆一下Qt的相关知识,特别是关于控件坐标的部分。 我记得Qt中的控件坐标系统有不同的方法,可能有局部坐标全局坐标之分。用户想知道的是全局坐标,也就是相对于屏幕的位置。可能需要用到某个特定的方法或者函数。 首先,控件的位置通常是相对于其父控件的,所以直接获取的位置可能是相对于父级的局部坐标。比如pos()函数返回的是相对于父控件的位置。那如何转换为全局坐标呢? 然后,想到Qt中可能有一个方法可以将局部坐标转换为全局坐标。比如mapToGlobal函数,这个函数应该可以将控件内的坐标转换为屏幕坐标。或者控件本身有一个方法直接获取全局位置,比如globalPos(),但不确定是否存在这样的方法。 另外,可能控件的位置包括框架,比如窗口的标题栏,这时候可能需要区分包含框架不包含框架的情况。比如,对于QWidget,有frameGeometry()geometry()的区别,但用户可能只关心控件本身的位置,而不是整个窗口的框架。 接着,正确的步骤可能是这样的:首先获取控件相对于其父控件的位置,然后将其转换为全局坐标。或者,控件有一个函数可以直接返回其在全局坐标系中的位置。例如,QWidget的mapToGlobal(QPoint(0,0)),这会将控件左上角的坐标转换为全局坐标。或者,使用控件的位置加上父控件的位置递归计算,但这样可能比较复杂,而Qt应该提供了直接的方法。 另外,还需要考虑控件是否已经显示出来,因为如果控件尚未显示,其坐标可能无效。但用户可能只需要代码层面的正确方法,不管实际显示与否。 可能的正确方法是使用mapToGlobal,并传递控件的原点(0,0)作为参数。例如,widget->mapToGlobal(QPoint(0,0)) 会返回该控件左上角在屏幕上的坐标。 或者,QWidget有一个方法叫做pos(),但那是相对于父控件的,所以需要找到相对于屏幕的位置。这时候可能需要将控件的位置转换为全局坐标,使用mapToGlobal函数。例如: QPoint globalPos = widget->mapToGlobal(widget->pos()); 不过,这样可能有问题,因为widget->pos()是相对于父控件的,而mapToGlobal是相对于控件自己的坐标系。正确的做法应该是将控件的原点(0,0)转换到全局坐标,因为控件的左上角在自己的坐标系中是(0,0),所以使用mapToGlobal(QPoint(0,0))会得到其在屏幕上的位置。 比如: QPoint globalPos = widget->mapToGlobal(QPoint(0, 0)); 或者,是否有其他方法,比如直接获取全局位置?比如,使用geometry()方法获取控件的位置,然后转换为全局坐标?不过这可能也需要转换。 可能需要举例说明,例如对于一个按钮控件,如何获取其在屏幕上的位置。 另外,可能还有另一个方法:使用QWidget的frameGeometry()或者geometry()返回的QRect的位置,然后转换为全局坐标。但同样需要转换函数。 总结下来,正确的做法应该是使用mapToGlobal函数,将控件内部的坐标(0,0)转换为全局坐标,从而得到控件左上角在屏幕上的位置。或者,是否存在更直接的方法,比如某个属性或函数直接返回全局位置? 另外,可能需要考虑窗口的装饰(比如标题栏),例如对于顶级窗口,它的pos()返回的是包括窗口装饰的位置吗?或者是否需要用geometry()来获取不包括装饰的位置? 不过用户的问题是关于获取控件全局坐标,不管该控件是否是顶级窗口。因此,无论控件处于哪个层次结构中,都需要正确计算其在屏幕上的位置。 所以,正确的代码示例应该是: QPoint globalPos = widget->mapToGlobal(QPoint(0, 0)); 或者,如果控件是顶级窗口,那么widget->pos()可能已经是屏幕坐标,但为了通用性,使用mapToGlobal可能更可靠,因为它会递归计算所有父控件的位置,直到得到屏幕坐标。 因此,结论是:使用mapToGlobal函数,参数是控件内的原点(0,0),从而得到全局坐标。另外,如果控件包含多个点,比如右下角,可以使用mapToGlobal来转换相应的点。 所以,回答用户的问题时,需要分步骤说明:首先说明局部坐标全局坐标的区别,然后介绍mapToGlobal方法,并给出示例代码,可能还需要提到相关的方法如mapFromGlobal,以及注意事项,比如确保控件已经添加到父控件并且显示,否则坐标可能不正确。</think>在Qt中获取控件全局坐标(即相对于屏幕的坐标)可以通过以下方法实现: --- ### **核心方法** 使用 `QWidget::mapToGlobal()` 函数,将控件的局部坐标转换为屏幕坐标。例如,获取控件左上角的全局位置: ```cpp QPoint globalPos = widget->mapToGlobal(QPoint(0, 0)); ``` --- ### **详细步骤** 1. **局部坐标 vs 全局坐标**: - 控件的 `pos()` 返回的是相对于父控件的位置(局部坐标)。 - 全局坐标控件左上角在屏幕上的绝对位置。 2. **转换方法**: - 调用控件的 `mapToGlobal(QPoint(0, 0))`,将控件自身的原点 `(0, 0)` 转换为屏幕坐标。 3. **获取控件尺寸**: - 使用 `widget->rect().bottomRight()` 可获取右下角局部坐标,再转换为全局坐标: ```cpp QPoint bottomRightGlobal = widget->mapToGlobal(widget->rect().bottomRight()); ``` --- ### **示例代码** ```cpp // 获取控件左上角的全局坐标 QPoint globalTopLeft = ui->pushButton->mapToGlobal(QPoint(0, 0)); // 获取控件右下角的全局坐标 QPoint globalBottomRight = ui->pushButton->mapToGlobal(ui->pushButton->rect().bottomRight()); // 输出结果 qDebug() << "Top-Left Screen Position:" << globalTopLeft; qDebug() << "Bottom-Right Screen Position:" << globalBottomRight; ``` --- ### **注意事项** - **控件可见性**:确保控件已添加到父控件并显示(`show()`),否则坐标可能无效。 - **顶级窗口**:如果是顶级窗口(如 `QMainWindow`),`pos()` 返回的是屏幕坐标,但仍建议使用 `mapToGlobal` 保持代码一致性。 - **坐标系差异**:窗口的 `geometry()` 包含边框标题栏,而 `frameGeometry()` 可能不同,需根据需求选择。 --- ### **反向操作** 若需要将屏幕坐标转换为控件内的局部坐标,使用 `mapFromGlobal()`: ```cpp QPoint localPos = widget->mapFromGlobal(globalPos); ``` 通过上述方法,可以准确获取控件在屏幕中的位置,适用于需要绝对定位的场景(如弹出菜单或拖放操作)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值