Qt 獲取控件位置座標,屏幕座標,相對父窗體座標

原文鏈接:https://blog.csdn.net/fanyun_01/article/details/78369874

1.QPoint QMouseEvent::pos()

這個只是返回相對這個widget(重載了QMouseEvent的widget)的位置。
const Returns the position of the mouse cursor, relative to the widgetthat 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 theevent. This is important on asynchronous window systems like X11. Whenever youmove your widgets around in response to mouse events, globalPos() may differ alot from the current pointer position QCursor::pos(), and fromQWidget::mapToGlobal(pos()).

3.QPoint QCursor::pos() [static]

返回相對顯示器的全局座標
Returns the position of the cursor (hot spot) of the primary screen inglobal screen coordinates. You can call QWidget::mapFromGlobal() to translateit to widget coordinates. Note: The position is queried from the windowingsystem. If mouse events are generated via other means (e.g., viaQWindowSystemInterface in a unit test), those fake mouse moves will not bereflected in the returned value. Note: On platforms where there is no windowingsystem or cursors are not available, the returned position is based on the mousemove events generated via QWindowSystemInterface.

4.QPoint QWidget::mapToGlobal(const QPoint & pos)  const

將窗口座標轉換成顯示器座標
Translates the widget coordinate pos to global screen coordinates. Forexample, mapToGlobal(QPoint(0,0)) would give the global coordinates of thetop-left pixel of the widget. See also mapFromGlobal(), mapTo(), andmapToParent().

5.QPoint QWidget::mapFromGlobal(const QPoint & pos) const

將顯示器座標轉換成窗口座標
Translates the global screen coordinate pos to widget coordinates.

6.QPoint QWidget::mapToParent(const QPoint & pos) const

將窗口座標獲得的pos轉換成父類widget的座標

Translates the widget coordinate pos to acoordinate in the parent widget.

7.QPoint QWidget::mapFromParent(const QPoint & pos) const

將父類窗口座標轉換成當前窗口座標
Translates the parent widget coordinate posto widget coordinates. Same as mapFromGlobal() if the widget has no parent.

8.QPoint QWidget::mapTo(const QWidget * parent, const QPoint &pos) const

將當前窗口座標轉換成指定parent座標。

Translates the widget coordinate pos to thecoordinate system of parent. The parent must not be 0 and must be a parent ofthe calling widget. See also mapFrom(), mapToParent(), mapToGlobal(), andunderMouse().

9.QWidget::pos() : QPoint

這個屬性獲得的是當前目前控件在父窗口中的位置,
This property holds the position of thewidget within its parent widget.

If the widget is a window, the position isthat of the widget on the desktop, including its frame.

When changing the position, the widget, ifvisible, receives a move event (moveEvent()) immediately. If the widget is notcurrently visible, it is guaranteed to receive an event before it is shown.

By default, this property contains aposition that refers to the origin.

Warning: Calling move() or setGeometry()inside moveEvent() can lead to infinite recursion.

See the Window Geometry documentation foran overview of geometry issues with windows.

10.const QPointF &QMouseEvent::screenPos() const

Returns the position of the mouse cursor asa QPointF, relative to the screen that received the event.
和QPoint QMouseEvent::globalPos() 值相同,但是類型更高精度的QPointF
This function was introduced in Qt 5.0.

11.獲取全局座標

QCursor::pos() == QMouseEvent::globalPos() 

12. 將鼠標的座標轉換成全局座標。

QMouseEvent::globalPos() == ui.posBtn->mapToGlobal(ui.posBtn->pos());

13.將全局座標(鼠標當前座標,QCursor::pos())直接轉換成當前窗口相對座標

ui.posBtn->mapFromGlobal(QCursor::pos());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章