【QT】QT5 获取窗口、系统屏幕大小尺寸信息,Qt 获取控件位置坐标,屏幕坐标,相对父窗体坐标

【QT】QT5 获取窗口、系统屏幕大小尺寸信息,Qt 获取控件位置坐标,屏幕坐标,相对父窗体坐标

一、QT5 获取窗口大小尺寸信息

QT窗口尺寸,窗口大小和大小改变引起的事件 QResizeEvent。
在这里插入图片描述
在这里插入图片描述

  //窗口左上角的位置(含边框)
   qDebug() << this->frameGeometry().x() << this->frameGeometry().y() << ;//1
   qDebug() << this->x()  << this->y();//2
   qDebug() << this->pos().x() << this->pos().y();//3
   //窗口的宽度和高度(含边框)
   qDebug() << this->frameGeometry().width() << this->frameGeometry().height();
   //窗口左上角的位置(不含边框)
   qDebug() << this->geometry().x() << this->geometry().y();
   //窗口的宽度和高度(不含边框)
   qDebug() << this->geometry().width() << this->geometry().height();//1
   qDebug() << this->width() << this->height();//2
   qDebug() << this->rect().width() << this->rect().height();//3
   qDebug() << this->size().width() << this->size().height();//4

二、QT获取系统屏幕大小

QDesktopWidget 提供了详细的位置信息,其能够自动返回窗口在用户窗口的位置和应用程序窗口的位置,

QDesktopWidget* pDesktopWidget = QApplication::desktop();
//获取可用桌面大小
QRect deskRect = QApplication::desktop()->availableGeometry();
//获取主屏幕分辨率
QRect screenRect = QApplication::desktop()->screenGeometry();
//获取屏幕数量
int nScreenCount = QApplication::desktop()->screenCount();

Qt5开始,QDesktopWidget官方不建议使用,改为QScreen。

#include<QScreen>
#include<QRect>
 
QList<QScreen *> list_screen =  QGuiApplication::screens();  //多显示器
QRect rect = list_screen.at(0)->geometry();
desktop_width = rect.width();
desktop_height = rect.height();
qDebug() << desktop_width <<desktop_height;

三、设置窗体大小

void setGeometry(int x, int y, int w, int h)

void setGeometry(const QRect &)

void resize(int w, int h)

void resize(const QSize &)

四、Qt 获取控件位置坐标,屏幕坐标,相对父窗体坐标

QPoint QMouseEvent::pos()

这个只是返回相对这个widget(重载了QMouseEvent的widget)的位置。
QPoint QMouseEvent::globalPos()

窗口坐标,这个是返回鼠标的全局坐标
QPoint QCursor::pos() [static]

返回相对显示器的全局坐标
QPoint QWidget::mapToGlobal(const QPoint & pos)  const

将窗口坐标转换成显示器坐标
QPoint QWidget::mapFromGlobal(const QPoint & pos) const

将显示器坐标转换成窗口坐标
QPoint QWidget::mapToParent(const QPoint & pos) const

将窗口坐标获得的pos转换成父类widget的坐标
QPoint QWidget::mapFromParent(const QPoint & pos) const

将父类窗口坐标转换成当前窗口坐标
QPoint QWidget::mapTo(const QWidget * parent, const QPoint &pos) const

将当前窗口坐标转换成指定parent坐标。
QWidget::pos() : QPoint

这个属性获得的是当前目前控件在父窗口中的位置,
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.
QCursor::pos() == QMouseEvent::globalPos() 

获取全局坐标
QMouseEvent::globalPos() == ui.posBtn->mapToGlobal(ui.posBtn->pos());

将鼠标的坐标转换成全局坐标。
ui.posBtn->mapFromGlobal(QCursor::pos());

将全局坐标(鼠标当前坐标,QCursor::pos())直接转换成当前窗口相对坐标
  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值