Qt获取控件位置,坐标总结

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

Qt获取控件位置,坐标总结 - 柳北风儿 - 柳北风儿~~~~~~~欲宇仙炅
 

上面的mouseEvent.globalPos()和QCursor::pos()永远相同,都是全局坐标。

上面绿色按钮的当前坐标:ui.pushButton->pos() 、转换父窗口坐标后mapToParent()、转换成全局坐标后mapToGlobal();如果当前鼠标坐标摸到按钮,按钮上面的文字会发生变化,经过比较。确实得到:QCursor::pos() == ui.posBtn->mapFromGlobal(QCursor::pos());



代码如下,跟踪任何控件,记得设置跟踪轨迹为真setMouseTracking(true)

  1. void TestWidget::mouseMoveEvent(QMouseEvent* event)  
  2. {  
  3.     QPoint m = event->globalPos();  
  4.     ui.lblMouseEventGlobalPos->setText(QString("(%1,%2)").arg(m.x()).arg(m.y()));  
  5.     QPoint n = QCursor::pos();  
  6.     ui.lblCursorPos->setText(QString("(%1,%2)").arg(n.x()).arg(n.y()));  
  7.     QPoint k = ui.posBtn->pos();  
  8.     QPoint h = ui.posBtn->mapToGlobal(ui.posBtn->pos());  
  9.     QPoint i = ui.posBtn->mapToGlobal(ui.posBtn->pos());  
  10.   
  11.     ui.lblPushBttonPos->setText(QString("(%1,%2)").arg(k.x()).arg(k.y()));  
  12.     ui.lblToParentPos->setText(QString("(%1,%2)").arg(h.x()).arg(h.y()));  
  13.     ui.lblToGlobalPos->setText(QString("(%1,%2)").arg(i.x()).arg(i.y()));  
  14.   
  15.   
  16.     QRect widgetRect = ui.posBtn->geometry();  
  17.     QPoint mousePos = ui.posBtn->mapFromGlobal(QCursor::pos());  
  18.     if (widgetRect.contains(mousePos))  
  19.     {  
  20.         ui.posBtn->setText("摸到我了");  
  21.     }  
  22.     else  
  23.     {  
  24.         ui.posBtn->setText("....");  
  25.     }  
  26.   
  27. }  

 


==========================基础知识=================
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. 
<think>好的,用户想了解如何在Qt获取控件坐标。我需要分步骤解释清楚,确保涵盖不同情况,并且结构清晰。 首先,要明确Qt中的坐标系。Qt使用的是相对于父控件坐标系统,原点在左上角,x向右,y向下。这点很重要,因为控件位置是相对于父容器的。 接下来,用户可能需要获取控件位置。常用的方法有pos(),返回的是相对于父控件QPoint。比如,一个按钮在父窗口中的位置,可以用button->pos()得到。 然后,用户可能想知道控件相对于窗口或屏幕的位置。这时候需要区分窗口坐标和屏幕坐标。使用mapToGlobal可以将控件位置转换为全局屏幕坐标。例如,button->mapToGlobal(QPoint(0,0))会得到该控件左上角在屏幕上的坐标。 另外,控件可能有多个父级,或者需要相对于其他控件坐标。这时可以用mapTo或mapFrom方法进行坐标转换。例如,将控件A的位置转换到控件B的坐标系中,可以用A->mapTo(B, A->pos())。 还有,如果用户想获取控件的几何信息,包括位置和大小,可以用geometry()方法,返回QRect。而frameGeometry()则包括窗口的边框。 需要注意的是,如果控件还没显示出来,获取坐标可能不正确。这时候应该等到控件显示后再获取,或者在resizeEvent等事件处理函数中调用。 最后,应该提醒用户注意父控件的影响,如果父控件移动,子控件位置也会变化。因此,坐标是动态的,可能需要实时更新。 总结步骤:先介绍坐标系,再讲获取相对父控件位置,然后转换到窗口或屏幕坐标,接着是与其他控件的转换,最后提及几何信息和注意事项。这样用户就能全面了解如何获取不同情况下的坐标了。</think>在Qt获取控件坐标主要涉及以下几个关键点和步骤: 1. **理解坐标系系统** - Qt使用**相对坐标系**,控件位置是相对于其父容件的 - 原点(0,0)位于父容器的左上角 - X轴向右延伸,Y轴向下延伸 2. **获取相对父容器的坐标** ```cpp QPoint relativePos = widget->pos(); // 返回QPoint对象 int x = relativePos.x(); int y = relativePos.y(); ``` - `pos()`方法返回控件相对于父容器的坐标 - 适用于大多数常规布局需求 3. **获取绝对屏幕坐标** ```cpp QPoint screenPos = widget->mapToGlobal(QPoint(0, 0)); ``` - `mapToGlobal()`将控件原点(0,0)转换为屏幕坐标 - 适用于需要系统级定位的场景(如弹出菜单) 4. **获取窗口内坐标** ```cpp QPoint windowPos = widget->mapTo(parentWindow, QPoint(0, 0)); ``` - 当需要相对于特定窗口定位时使用 - 可指定任意参考容器进行坐标转换 5. **几何信息获取** ```cpp QRect geometry = widget->geometry(); // 获取包含边框的几何信息 QRect frameGeo = widget->frameGeometry(); // 包含窗口装饰的几何信息 ``` 6. **注意事项** - 在控件显示前(`show()`调用前)获取坐标可能无效 - 布局管理器可能会修改控件位置,建议在`resizeEvent`或`showEvent`中获取 - 使用`QWidget::move()`方法可以动态设置控件位置 **完整示例代码**: ```cpp // 获取相对于父容器的坐标 QPoint relativePos = ui->pushButton->pos(); qDebug() << "Relative position:" << relativePos; // 获取屏幕绝对坐标 QPoint globalPos = ui->pushButton->mapToGlobal(QPoint(0,0)); qDebug() << "Screen position:" << globalPos; // 获取控件尺寸 QRect geometry = ui->pushButton->geometry(); qDebug() << "Width:" << geometry.width() << "Height:" << geometry.height(); ``` 通过理解这些坐标系统的差异,结合具体的应用场景选择合适的方法,可以准确获取和操作控件位置信息。对于动态布局的界面,建议结合Qt的信号槽机制实时更新坐标信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值