Qt获取屏幕等的信息

窗口大小信息

使用三个函数:
setGeometry(), resize(), move().

参见

控件的位置信息

返回相对于Widget的位置:

QPoint QMouseEvent::pos()

鼠标的全局坐标:

QPoint QMouseEvent::globalPos()
const QPointF &QMouseEvent::screenPos() const

相对显示器的坐标

QPoint QCursor::pos() [static]
它和前面的QMouseEvent::globalPos()相等.

窗口坐标转换成显示器坐标

QPoint QWidget::mapToGlobal(const QPoint & pos) const

显示器坐标转换成窗口坐标

QPoint QWidget::mapFromGlobal(const QPoint & pos) const

窗口坐标转换成父类widget的坐标

QPoint QWidget::mapToParent(const QPoint & pos) const

将父类窗口坐标转换成当前窗口坐标

QPoint QWidget::mapFromParent(const QPoint & pos) const

当前窗口坐标转换成指定parent的坐标

QPoint QWidget::mapTo(const QWidget * parent, const QPoint &pos) const

当前控件在父窗口的坐标

QWidget::pos() : QPoint

获取系统屏幕大小

Qt5之前,使用QDesktopWidget, Qt5之后, 使用QScreen.

1
2
3
4
5
6
7
8
9
#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;