窗口坐标 专栏收录该内容
4 篇文章 0 订阅


1、QPoint QMouseEvent::pos() 


       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


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 


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() 都是全局坐标;


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


当前窗口相对坐标 ==  ui.posBtn->mapFromGlobal(QCursor::pos());



上面绿色按钮的当前坐标:ui.pushButton->pos() 、转换父窗口坐标后mapToParent()、转换成全局坐标后mapToGlobal();


确实得到:QCursor::pos() == ui.posBtn->mapFromGlobal(QCursor::pos());

  • 0
  • 1
  • 4
  • 一键三连
  • 扫一扫,分享海报

该控件继承QWidget, 实现了左右上下四种形式的坐标轴控件。 可以设置固定间隔或自动选择间隔 可以设置最小间隔 开放一个槽来动态调整坐标轴的范围 处理了边缘刻度的显示 /************************************************************************ * 版权所有 (C) 2012-2015, liang1057@yahoo.com.cn 类声明: 坐标轴控件 ************************************************************************/ /** @brief 坐标轴控件 * * @details 坐标轴控件 只有刻度数字,数字可以隐藏(用来显示其他需要显示的刻度值) */ class uiAxis : public QWidget { Q_OBJECT public: /** @brief 坐标轴类型 * * @details 坐标轴类型 */ enum AXISTYPE{ LEFT_AXIS = 0, TOP_AXIS, RIGHT_AXIS, BOTTOM_AXIS }; /** @brief 构造函数 */ uiAxis(AXISTYPE type = BOTTOM_AXIS, QWidget *parent = 0); /** @brief 析构函数 */ ~uiAxis(void); /** @brief 设置坐标轴的范围 */ void setScop(double minValue, double maxValue); /** @brief 获取坐标轴的范围 */ void getScop(double& minValue,double& maxValue); /** @brief 获取坐标轴的范围 */ double getMinValue(); double getMaxValue(); /** @brief 设置坐标轴的类型 */ void setAxisType(AXISTYPE type); /** @brief 坐标轴的类型 */ AXISTYPE getAxisType(); /** @brief 设置最小刻度(小刻度的最小间隔) */ void setMinInterval(double value); /** @brief 设置自动间隔 */ void setAutoScale(bool val=true); /** @brief 设置固定间隔 */ void setSettedScale(bool val=true); /** @brief 设置坐标轴的绘制范围, 像素值 */ void setBoundary(int left, int right, int top, int bottom); void getBoundary(int& left, int& right, int& top, int& bottom);
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
钱包余额 0