QScreen Class

QScreen 类用于查询屏幕属性。更多

Header: #include <QScreen>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QObject

属性

公共功能

int angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b) const
QRect availableGeometry() const
QSize availableSize() const
QRect availableVirtualGeometry() const
QSize availableVirtualSize() const
int depth() const
qreal devicePixelRatio() const
QRect geometry() const
QPixmap grabWindow(WId window = 0, int x = 0, int y = 0, int width = -1, int height = -1)
QPlatformScreen *handle() const
bool isLandscape(Qt::ScreenOrientation o) const
bool isPortrait(Qt::ScreenOrientation o) const
qreal logicalDotsPerInch() const
qreal logicalDotsPerInchX() const
qreal logicalDotsPerInchY() const
QString manufacturer() const
QRect mapBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &rect) const
QString model() const
QString name() const
QNativeInterface *nativeInterface() const
Qt::ScreenOrientation nativeOrientation() const
Qt::ScreenOrientation orientation() const
qreal physicalDotsPerInch() const
qreal physicalDotsPerInchX() const
qreal physicalDotsPerInchY() const
QSizeF physicalSize() const
Qt::ScreenOrientation primaryOrientation() const
qreal refreshRate() const
QString serialNumber() const
QSize size() const
QTransform transformBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &target) const
QRect virtualGeometry() const
QScreen *virtualSiblingAt(QPoint point)
QList<QScreen *> virtualSiblings() const
QSize virtualSize() const

信号

void availableGeometryChanged(const QRect &geometry)
void geometryChanged(const QRect &geometry)
void logicalDotsPerInchChanged(qreal dpi)
void orientationChanged(Qt::ScreenOrientation orientation)
void physicalDotsPerInchChanged(qreal dpi)
void physicalSizeChanged(const QSizeF &size)
void primaryOrientationChanged(Qt::ScreenOrientation orientation)
void refreshRateChanged(qreal refreshRate)
void virtualGeometryChanged(const QRect &rect)

详细说明

关于每英寸逻辑点数与每英寸物理点数的说明:物理 DPI 基于可用的实际物理像素尺寸,适用于打印预览和其他需要了解屏幕显示内容确切物理尺寸的情况。

每英寸逻辑点数用于将字体和用户界面元素从点尺寸转换为像素尺寸,可能与每英寸物理点数不同。每英寸逻辑点数有时可由用户在桌面环境的设置面板中设置,以便用户在不同的应用程序中全局控制用户界面和字体大小。

注: 物理和逻辑 DPI 均以独立于设备的点数表示。乘以QScreen::devicePixelRatio() 即可得到与设备相关的密度。

属性文档

[read-only] availableGeometry : const QRect

此属性以像素为单位保存屏幕的可用几何图形

可用几何图形是不包括窗口管理器保留区域(如任务栏和系统菜单)的几何图形。

注意,在 X11 系统上,只有在有一个显示器且窗口管理器设置了 _NET_WORKAREA 原子的情况下,才会返回真正的可用几何图形。在所有其他情况下,该值等于geometry()。这是 X11 窗口管理器规范的限制。

访问功能:

QRect availableGeometry() const

Notifier 信号:

void availableGeometryChanged(const QRect &geometry)

[read-only] availableSize : const QSize

该属性表示屏幕的可用尺寸,单位为像素

可用尺寸是指不包括窗口管理器保留区域(如任务栏和系统菜单)的尺寸。

访问功能:

QSize availableSize() const

通知信号:

void availableGeometryChanged(const QRect &geometry)

[read-only] availableVirtualGeometry : const QRect

此属性保存此屏幕所属虚拟桌面的可用几何图形

返回此屏幕对应的虚拟桌面的可用几何图形。

这是虚拟兄弟姐妹的单个可用几何图形的组合。

访问功能:

QRect availableVirtualGeometry() const

通知信号:

void virtualGeometryChanged(const QRect &rect)

另请参阅 availableGeometry() 和virtualSiblings()。

[read-only] availableVirtualSize : const QSize

此属性保存此屏幕所属虚拟桌面的可用尺寸

返回此屏幕对应的虚拟桌面的可用像素尺寸。

这是虚拟兄弟姐妹各自可用几何尺寸的总和。

访问功能:

QSize availableVirtualSize() const

通知信号:

void virtualGeometryChanged(const QRect &rect)

另请参阅 availableSize() 和virtualSiblings()。

[read-only] depth : const int

此属性保存屏幕的色彩深度

访问功能

int depth() const

[read-only] devicePixelRatio : const qreal

此属性表示屏幕的物理像素与设备无关像素的比例

返回屏幕的物理像素与设备无关像素之间的比例。

普通显示屏的常用值为 1.0,"视网膜 "显示屏的常用值为 2.0。也可以使用更高的值。

注意: 在某些平台上,窗口的 devicePixelRatio 和它所在屏幕的 devicePixelRatio 可能不同。只有在不知道目标窗口时才使用此功能。如果知道目标窗口,请使用QWindow::devicePixelRatio() 代替。

访问函数:

qreal devicePixelRatio() const

通知信号:

void physicalDotsPerInchChanged(qreal dpi)

另请参阅 QWindow::devicePixelRatio() 和QGuiApplication::devicePixelRatio()。

[read-only] geometry : const QRect

该属性以像素为单位显示屏幕的几何尺寸。

例如,它可能返回QRect(0, 0, 1280, 1024),或者在虚拟桌面设置中返回QRect(1280, 0, 1280, 1024)。

访问功能:

QRect geometry() const

通知信号:

void geometryChanged(const QRect &geometry)

[read-only] logicalDotsPerInch : const qreal

该属性表示每英寸的逻辑点数或像素数。

该值可用于将字体点尺寸转换为像素尺寸。

这是一个方便的属性,只是logicalDotsPerInchXlogicalDotsPerInchY 属性的平均值。

访问功能:

qreal logicalDotsPerInch() const

Notifier 信号:

void logicalDotsPerInchChanged(qreal dpi)

另请参阅 logicalDotsPerInchX() 和logicalDotsPerInchY()。

[read-only] logicalDotsPerInchX : const qreal

该属性表示水平方向上每英寸的逻辑点数或像素数。

该值用于将字体点尺寸转换为像素尺寸。

访问功能:

qreal logicalDotsPerInchX() const

Notifier 信号:

void logicalDotsPerInchChanged(qreal dpi)

另请参阅 logicalDotsPerInchY().

[read-only] logicalDotsPerInchY : const qreal

该属性保存垂直方向上每英寸的逻辑点数或像素数。

该值用于将字体点尺寸转换为像素尺寸。

访问功能:

qreal logicalDotsPerInchY() const

Notifier 信号:

void logicalDotsPerInchChanged(qreal dpi)

另请参阅 logicalDotsPerInchX().

[read-only] manufacturer : const QString

此属性保存屏幕的制造商

访问功能:

QString manufacturer() const

[read-only] model : const QString

此属性保存屏幕的模型

访问功能:

QString model() const

[read-only] name : const QString

该属性包含代表屏幕的用户可显示字符串

例如,在 X11 上,这些字符串与 XRandr 屏幕名称相对应,通常为 "VGA1"、"HDMI1 "等。

注意: 用户可显示的字符串不能保证与任何本地 API 的结果相匹配,也不能用于唯一标识屏幕。

访问功能:

QString name() const

[read-only] nativeOrientation : const Qt::ScreenOrientation

该属性用于保存原生屏幕方向

屏幕原生方向是指设备徽标贴纸朝上显示的方向,如果平台不支持此功能,则为Qt::PrimaryOrientation

本机方向是硬件的属性,不会改变。

访问功能:

Qt::ScreenOrientation nativeOrientation() const

[read-only] orientation : const Qt::ScreenOrientation

该属性显示屏幕方向

orientation 属性从窗口系统的角度显示屏幕的方向。

大多数移动设备和平板电脑都包含加速度传感器。Qt Sensors 模块提供了直接读取该传感器的功能。不过,窗口系统可能会根据握持方式自动旋转整个屏幕;在这种情况下,该orientation 属性将发生变化。

访问功能:

Qt::ScreenOrientation orientation() const

通知信号:

void orientationChanged(Qt::ScreenOrientation orientation)

另请参阅 primaryOrientation() 和QWindow::contentOrientation()。

[read-only] physicalDotsPerInch : const qreal

该属性表示每英寸的物理点数或像素数。

该值表示屏幕显示的像素密度。根据底层系统提供的信息,该值可能并不完全准确。

这是一个方便的属性,只是physicalDotsPerInchXphysicalDotsPerInchY 属性的平均值。

注: 物理 DPI 用与设备无关的点表示。乘以QScreen::devicePixelRatio() 即可得到与设备相关的密度。

访问功能:

qreal physicalDotsPerInch() const

通知信号:

void physicalDotsPerInchChanged(qreal dpi)

另请参阅 physicalDotsPerInchX() 和physicalDotsPerInchY()。

[read-only] physicalDotsPerInchX : const qreal

该属性表示水平方向上每英寸的物理点数或像素数。

该值表示屏幕显示的实际水平像素密度。根据底层系统提供的信息,该值可能并不完全准确。

注: 物理 DPI 用与设备无关的点数表示。乘以QScreen::devicePixelRatio() 即可得到与设备相关的密度。

访问功能:

qreal physicalDotsPerInchX() const

Notifier 信号:

void physicalDotsPerInchChanged(qreal dpi)

另请参见 physicalDotsPerInchY()。

[read-only] physicalDotsPerInchY : const qreal

该属性表示垂直方向上每英寸的物理点数或像素数。

该值表示屏幕显示的实际垂直像素密度。根据底层系统提供的信息,该值可能并不完全准确。

注: 物理 DPI 用与设备无关的点数表示。乘以QScreen::devicePixelRatio() 即可得到与设备相关的密度。

访问功能:

qreal physicalDotsPerInchY() const

Notifier 信号:

void physicalDotsPerInchChanged(qreal dpi)

另请参见 physicalDotsPerInchX()。

[read-only] physicalSize : const QSizeF

此属性保存屏幕的物理尺寸(单位:毫米)

物理尺寸表示屏幕显示的实际物理尺寸。

根据底层系统提供的信息,该值可能并不完全准确。

访问功能:

QSizeF physicalSize() const

通知信号:

void physicalSizeChanged(const QSizeF &size)

[read-only] primaryOrientation : const Qt::ScreenOrientation

该属性用于保存主屏幕方向

如果屏幕几何体的宽度大于或等于高度,则主屏幕方向为Qt::LandscapeOrientation ,否则为Qt::PortraitOrientation 。当屏幕方向改变时(即旋转显示屏时),该属性可能会改变。不过,该行为与平台有关,通常可在应用程序清单文件中指定。

访问功能:

Qt::ScreenOrientation primaryOrientation() const

通知信号:

void primaryOrientationChanged(Qt::ScreenOrientation orientation)

[read-only] refreshRate : const qreal

该属性保存屏幕的大致垂直刷新率(单位 Hz

警告: 避免使用屏幕刷新率通过定时器(如QChronoTimer )驱动动画。请使用QWindow::requestUpdate() 。

访问功能:

qreal refreshRate() const

Notifier 信号:

void refreshRateChanged(qreal refreshRate)

另请参见 QWindow::requestUpdate().

[read-only] serialNumber : const QString

该属性保存屏幕的序列号

访问功能:

QString serialNumber() const

[read-only] size : const QSize

此属性保存屏幕的像素分辨率

访问功能:

QSize size() const

通知信号

void geometryChanged(const QRect &geometry)

[read-only] virtualGeometry : const QRect

此属性保存此屏幕所属虚拟桌面的像素几何图形

返回此屏幕对应的虚拟桌面的像素几何图形。

这是虚拟兄弟姐妹各自几何图形的组合。

访问功能:

QRect virtualGeometry() const

通知信号:

void virtualGeometryChanged(const QRect &rect)

另请参阅 virtualSiblings().

[read-only] virtualSize : const QSize

此属性保存此屏幕所属虚拟桌面的像素大小

返回此屏幕对应的虚拟桌面的像素大小。

这是虚拟兄弟姐妹各自几何尺寸的总和。

访问功能:

QSize virtualSize() const

通知信号:

void virtualGeometryChanged(const QRect &rect)

另请参阅 virtualSiblings().

成员函数文档

int QScreen::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b) const

方便函数,用于计算从旋转a 到旋转b 的旋转角度。

结果将是 0、90、180 或 270。

Qt::PrimaryOrientation 被解释为屏幕的 ().primaryOrientation

QPixmap QScreen::grabWindow(WId window = 0, int x = 0, int y = 0, int width = -1, int height = -1)

抓取window 中受QRect 限制的内容(x,y,width,height ),创建并返回像素图。如果window 为 0,则将抓取整个屏幕。

参数 (x,y) 指定了窗口中的偏移量,而 (width,height) 则指定了要复制的区域。如果width 为负值,函数将复制窗口右边界的所有内容。如果height 为负数,则函数会将所有内容复制到窗口底部。

偏移和大小参数以独立于设备的像素为单位指定。从高分辨率屏幕抓取时,返回的像素图可能大于请求的大小。请调用QPixmap::devicePixelRatio() 来确定情况是否如此。

窗口系统标识符 (WId) 可通过QWidget::winId() 函数获取。使用窗口标识符而不是QWidget ,是为了抓取不属于应用程序、窗口系统框架等的窗口。

警告: iOS 等系统不支持抓取不属于应用程序的窗口,因为在这些系统中,沙盒/安全机制会阻止读取不属于应用程序的窗口像素。

grabWindow() 函数从屏幕而非窗口抓取像素,也就是说,如果抓取的窗口部分或全部被另一个窗口覆盖,则也会从覆盖的窗口获取像素。鼠标指针一般不会被抓取。

请注意,在 X11 上,如果给定的window 与根窗口的深度不一样,并且另一个窗口部分或全部遮挡了您抓取的窗口,您将不会从覆盖的窗口获取像素。像素图中被遮挡区域的内容将是未定义和未初始化的。

在 Windows Vista 及以上版本中,抓取通过设置Qt::WA_TranslucentBackground 属性创建的分层窗口将不起作用。取而代之的是抓取桌面部件。

警告: 一般来说,抓取屏幕外的区域并不安全。这取决于底层窗口系统。

QPlatformScreen *QScreen::handle() const

获取平台屏幕句柄。

另请参阅 Qt Platform Abstraction (QPA)

bool QScreen::isLandscape(Qt::ScreenOrientation o) const

方便函数,如果o 是横向或倒置横向,则返回true ;否则返回false

Qt::PrimaryOrientation 被解释为屏幕的 ().primaryOrientation

bool QScreen::isPortrait(Qt::ScreenOrientation o) const

方便函数,如果o 是纵向或倒纵向,则返回true ;否则返回false

Qt::PrimaryOrientation 被解释为屏幕的 ().primaryOrientation

QRect QScreen::mapBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &rect) const

在两个屏幕方向之间映射矩形。

如果a 的方向是Qt::PortraitOrientationQt::InvertedPortraitOrientation ,而b 的方向是Qt::LandscapeOrientationQt::InvertedLandscapeOrientation ,则会翻转矩形rect 的 x 和 y 尺寸,反之亦然。

Qt::PrimaryOrientation 被解释为屏幕的 ().primaryOrientation

template <typename QNativeInterface> QNativeInterface *QScreen::nativeInterface() const

返回屏幕给定类型的本地接口。

该函数可访问QScreen 的平台特定功能,这些功能在QNativeInterface 命名空间中定义:

QNativeInterface::QAndroidScreen

屏幕的本地接口

QNativeInterface::QWaylandScreen

Wayland 上屏幕的本地接口

QNativeInterface::QWindowsScreen

屏幕的本地接口

如果请求的接口不可用,则返回nullptr

[signal] void QScreen::orientationChanged(Qt::ScreenOrientation orientation)

当以orientation 作为参数的屏幕方向发生变化时,将发出该信号。

注: 属性orientation 的通知信号。

另请参阅 orientation().

[signal] void QScreen::primaryOrientationChanged(Qt::ScreenOrientation orientation)

当以orientation 作为参数的屏幕主方向发生变化时,将发出该信号。

注: 属性primaryOrientation 的通知信号。

另请参阅 primaryOrientation().

QTransform QScreen::transformBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b, const QRect &target) const

方便函数,用于计算从方向a 定义的坐标系映射到方向b 和目标尺寸target 定义的坐标系的变换。

例如,a 是 Qt::Landscape,b 是 Qt::Portrait,targetQRect(0, 0, w, h),则生成的变换将使点QPoint(0, 0) 映射到QPoint(0, w),并使QPoint(h, w) 映射到QPoint(0, h)。因此,横向坐标系QRect(0, 0, h, w) 被映射(旋转 90 度)到纵向坐标系QRect(0, 0, w, h)。

Qt::PrimaryOrientation 被解释为屏幕的 ().primaryOrientation

QScreen *QScreen::virtualSiblingAt(QPoint point)

返回QScreen::virtualSiblings() 中point 处的屏幕,如果在任何屏幕之外,则返回nullptr

point 与每组虚拟兄弟姐妹的virtualGeometry() 有关。

QList<QScreen *> QScreen::virtualSiblings() const

获取屏幕的虚拟兄弟姐妹。

虚拟同级屏幕是共享同一虚拟桌面的屏幕实例。它们共享一个共同的坐标系,窗口可以在它们之间自由移动或定位,而无需重新创建。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.