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)

void TestWidget::mouseMoveEvent(QMouseEvent* event)  
{  
    QPoint m = event->globalPos();  
    ui.lblMouseEventGlobalPos->setText(QString("(%1,%2)").arg(m.x()).arg(m.y()));  
    QPoint n = QCursor::pos();  
    ui.lblCursorPos->setText(QString("(%1,%2)").arg(n.x()).arg(n.y()));  
    QPoint k = ui.posBtn->pos();  
    QPoint h = ui.posBtn->mapToGlobal(ui.posBtn->pos());  
    QPoint i = ui.posBtn->mapToGlobal(ui.posBtn->pos());  
  
    ui.lblPushBttonPos->setText(QString("(%1,%2)").arg(k.x()).arg(k.y()));  
    ui.lblToParentPos->setText(QString("(%1,%2)").arg(h.x()).arg(h.y()));  
    ui.lblToGlobalPos->setText(QString("(%1,%2)").arg(i.x()).arg(i.y()));  
  
  
    QRect widgetRect = ui.posBtn->geometry();  
    QPoint mousePos = ui.posBtn->mapFromGlobal(QCursor::pos());  
    if (widgetRect.contains(mousePos))  
    {  
        ui.posBtn->setText("摸到我了");  
    }  
    else  
    {  
        ui.posBtn->setText("....");  
    }  
  
}  

 

 

 

==========================基礎知識=================
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. 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. 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM