qt获取当前窗口的dpi--QOpenGL

本文探讨了在Qt中处理高DPI问题的方法,特别是针对OpenGL应用。文章介绍了devicePixelRatio的概念及其作用,并提供了一个示例来说明如何获取当前OpenGL窗口所在屏幕的devicePixelRatio。

目的

对于多屏处理烦恼太多了, 比如说多屏并且每个屏幕的dpi 缩放与布局不一样, 很容易会造成一些问题, 以及我们遇到的high dpi问题。 这篇主要是说QOpenGL high dpi 处理, devicePixelRatio。

Qt supports a high DPI mode where the main coordinate system is virtualized and made independent of the display pixel density. This mode is implemented by some operating systems (macOS, iOS). In addition, Qt contains an implementation which may be used where operating system support is missing.
Geometry is now specified in device independent pixels. This includes widget and item geometry, event geometry, desktop, window and screen geometry, and animation velocities. Rendered output is in device pixels, which corresponds to the display resolution. The ratio between the device independent and device pixel coordinate systems is the devicePixelRatio.
Applications mostly work with device independent pixels. Notable exceptions are OpenGL and code that works with raster graphics. 

Qt 支持高 DPI 模式,其中主坐标系被虚拟化并独立于显示像素密度。 此模式由某些操作系统(macOS、iOS)实现。 此外,Qt 包含一个可以在缺少操作系统支持的情况下使用的实现。
现在在设备独立像素中指定几何图形。 这包括小部件和项目几何、事件几何、桌面、窗口和屏幕几何以及动画速度。 渲染输出以设备像素为单位,对应于显示分辨率。 设备独立和设备像素坐标系之间的比率是 devicePixelRatio。
应用程序主要使用与设备无关的像素。 值得注意的例外是 OpenGL 和使用光栅图形的代码。

opengl 渲染输出 是像素显示分辨率, 在面对多屏, high dpi时, 需要devicePixelRatio(), 物理像素与设备无关像素之间比例, 方便做对应窗口大小,geometry等处理。

This is the ratio between physical pixels and device-independent pixels.

那么问题来了, 如何才能获取当前opengl 窗口所在的屏幕的 devicepixelratio?

解决方案

  qeal m_dpi = 1.0;
  int screenNum =  qApp->desktop()->screenNumber(this); //this 表示当前的窗口 QWidget类型
    if(screenNum >= 0){
        QScreen* screen = qApp->screens().at(screenNum);
        m_dpi = screen->devicePixelRatio();
    }

核心接口

int QDesktopWidget::screenNumber(const QWidget *widget = nullptr) const
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道阻且长,行则降至

无聊,打赏求刺激而已

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值