Qt 获取窗口、系统屏幕大小尺寸信息,Qt获取控件位置坐标、屏幕坐标,相对父窗体坐标
 
[toc]
Qt 窗口尺寸,窗口大小和大小改变引起的事件 QResizeEvent。
1 2 3 4 5 6 7 8 9 10 11 12 13 qDebug () << this ->frameGeometry ().x () << this ->frameGeometry ().y () << ;qDebug () << this ->x ()  << this ->y ();qDebug () << this ->pos ().x () << this ->pos ().y ();qDebug () << this ->frameGeometry ().width () << this ->frameGeometry ().height ();qDebug () << this ->geometry ().x () << this ->geometry ().y ();qDebug () << this ->geometry ().width () << this ->geometry ().height ();qDebug () << this ->width () << this ->height ();qDebug () << this ->rect ().width () << this ->rect ().height ();qDebug () << this ->size ().width () << this ->size ().height ();
QDesktopWidget 提供了详细的位置信息,其能够自动返回窗口在用户窗口的位置和应用程序窗口的位置。
1 2 3 4 5 6 7 QDesktopWidget* pDesktopWidget = QApplication::desktop ();desktop ()->availableGeometry ();desktop ()->screenGeometry ();int  nScreenCount = QApplication::desktop ()->screenCount ();
Qt5开始, QDesktopWidget官方不建议使用,改为 QScreen
1 2 3 4 5 6 7 8 #include <QScreen>  #include <QRect>  screens ();  at (0 )->geometry ();width ();height ();qDebug () << desktop_width <<desktop_height;
1 2 3 4 5 6 7 void  setGeometry (int  x, int  y, int  w, int  h) void  setGeometry (const  QRect &) void  resize (int  w, int  h) void  resize (const  QSize &) 
这个只是返回相对这个 widget (重载了QMouseEvent的widget)的位置。 
 
1 QPoint QMouseEvent::pos ()  
窗口坐标,这个是返回鼠标的全局坐标 
 
1 QPoint QMouseEvent::globalPos ()  
相对显示器的全局坐标 
 
1 QPoint QCursor::pos ()  [static ] 
将窗口坐标转换成显示器坐标 
 
1 QPoint QWidget::mapToGlobal (const  QPoint & pos)   const  
将显示器坐标转换成窗口坐标 
 
1 QPoint QWidget::mapFromGlobal (const  QPoint & pos)  const  
将窗口坐标获得的pos转成父类widget的坐标 
 
1 QPoint QWidget::mapFromParent (const  QPoint & pos)  const  
将当前窗口坐标转换成指定parent坐标 
 
1 QPoint QWidget::mapTo (const  QWidget * parent, const  QPoint &pos)  const  
这个属性获得的是当前目前控件在父窗口中的位置 
 
1 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 
 
1 const  QPointF &QMouseEvent::screenPos ()  const 
获取全局坐标 
 
1 QCursor::pos () == QMouseEvent::globalPos () 
将鼠标的坐标转换成全局坐标 
 
1 QMouseEvent::globalPos () == ui.posBtn->mapToGlobal (ui.posBtn->pos ());
将鼠标坐标(鼠标当前坐标,QCursor::pos())直接转换成当前窗口相对坐标 
 
1 ui.posBtn->mapFromGlobal (QCursor::pos ());