Qt 之获取控件位置坐标详解

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());
————————————————
版权声明:本文为CSDN博主「老樊Lu码」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fanyun_01/article/details/78369874

控件继承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);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值