Qt获取屏幕DPI
Qt在使用一些功能时需要用到DPI
Qt5.6已经可以自适应DPI与手动设置DPI
方案:
# 实测MSVC与MinGW都可运行 #include <QPaintDevice> //逻辑DPI int horizontalDPI = logicalDpiX(); int verticalDPI = logicalDpiY(); //物理DPI (和逻辑DPI不一定相同)这个是屏幕的真实DPI int horizontalDPI = physicalDpiX(); int verticalDPI = physicalDpiY();
下面是参考网上的方法的讨论!
# 1. 使用MSVC // Get desktop dc HDC desktopDc = GetDC(NULL); // Get native resolution float horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX); float verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY); int dpi = (horizontalDPI + verticalDPI) / 2; int fontsize = 4 * dpi / 72; //8pt 144dpi is 4px fontsize = fontsize > 8 ? fontsize : 8; QFont MenuFont("SimHei", fontsize); QApplication::setFont(MenuFont); 另一种: #include <QScreen> QScreen *screen = QGuiApplication::screens()[0]; //屏幕分辨率宽度 qreal dpiX = screen->physicalDotsPerInchX(); qreal dpiY = screen->physicalDotsPerInchY(); qreal dpi = screen->physicalDotsPerInch(); qDebug() << "dpi:" << dpi << " dpiX:" << dpiX << " dpiY:" << dpiY; # 2. 使用 MinGW #include <QPaintDevice> //逻辑DPI int horizontalDPI = logicalDpiX(); int verticalDPI = logicalDpiY(); //物理DPI (和逻辑DPI不一定相同)这个是屏幕的真实DPI int horizontalDPI = physicalDpiX(); int verticalDPI = physicalDpiY(); # 3. 分辨率 #include <QDesktopWidget> int currentScreenWidth = QApplication::desktop()->width(); int currentScreenHeight = QApplication::desktop()->height(); //或者 QDesktopWidget* desktopWidget = QApplication::desktop(); //获取可用桌面大小 QRect deskRect = desktopWidget->availableGeometry(); //获取设备屏幕大小 QRect screenRect = desktopWidget->screenGeometry(); screenX = screenRect.width(); screenY = screenRect.height();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧