Windows Qt动态监测系统分辨率及缩放比变化

前言

Windows 显示设置中,可以修改缩放比,所有界面和文字会同比例放大或缩小,在开发桌面程序时, 实时监测Qt应用程序在不同缩放比例下的表现,可以及时调整程序界面以适应不同显示屏幕的需求。

正文

本文通过Qt相关接口实时监测系统分辨率和缩放比的变化,并通过发送信号通知UI进行处理。兼容多屏情况,会自动检测软件窗口所在的屏幕。

关键代码:

cpp 复制代码
// HighDpiHelper.h
#ifndef HIGHDPIHELPER_H
#define HIGHDPIHELPER_H

#include "qfont.h"
#include "qscreen.h"
#include <QObject>
#include <QWindow>
#include <QGuiApplication>
#include <QDebug>

/**
 * @brief The HighDpiHelper class
 * @author luoyayun361
 * 自动监测程序所在的屏幕 缩放比变化
 * 使用需要先调用 dpiHelper.setWindow(mainWindow),mainWindow是主窗口id
 */

class HighDpiHelper : public QObject
{
    Q_OBJECT

public:
    HighDpiHelper() {
        //适配多屏幕的情况,只监测程序窗口所在的屏幕
        for(auto &screen : qApp->screens()){
            connect(screen,&QScreen::logicalDotsPerInchChanged,this,[=](qreal dpi){
                if(!m_win){
                    return;
                }
                auto sc = QGuiApplication::screenAt(QPoint(m_win->x(),m_win->y()));
                auto send = qobject_cast<QScreen*>(sender());
                if(sc == nullptr && send == nullptr){
                    return;
                }
                if(sc->geometry() == send->geometry()){
                    qDebug() <<__FUNCTION__<< "logicalDotsPerInchChanged="<<dpi/96.;
                    emit dpiChanged(dpi/96.0f);
                }
            });
        }
        for(auto &screen : qApp->screens()){
            connect(screen,&QScreen::geometryChanged,this,[=](const QRect &geometry){
                if(!m_win){
                    return;
                }
                auto sc = QGuiApplication::screenAt(QPoint(m_win->x(),m_win->y()));
                auto send = qobject_cast<QScreen*>(sender());
                if(sc == nullptr && send == nullptr){
                    return;
                }
                if(sc->geometry() == send->geometry()){
                    emit screenGeometryChanged(geometry);
                }
            });
        }
    }

    Q_INVOKABLE void setWindow(QWindow *win){
        m_win = win;
    }

    //获取当前屏幕缩放比
    Q_INVOKABLE float getScreenScalingFactor() {
        auto sc = QGuiApplication::screenAt(QPoint(m_win->x(),m_win->y()));
        if(sc){
            return sc->logicalDotsPerInch() / 96.0f; // 96 dpi 是 Windows 的标准DPI,所有的缩放比都是相对于标准 DPI 的
        }
        else{
            return 1.0;
        }
    }

    //获取窗口所在的屏幕尺寸
    Q_INVOKABLE QSize getScreenAvailableSize() {
        auto sc = QGuiApplication::screenAt(QPoint(m_win->x(),m_win->y()));
        if(sc){
            return sc->availableSize();
        }
        else{
            return qApp->screens().first()->availableSize();
        }
    }

signals:
    void dpiChanged(float scaleFactor);
    void screenGeometryChanged(const QRect &geometry);
private:
    QWindow *m_win = nullptr;
};
#endif // HIGHDPIHELPER_H

如果是在QWidget项目中,可以将QWindow 改成QWidget,然后将主窗口指针传进来即可。QML项目的话 直接使用QWindow就行。附件提供了两种场景下的demo调用。

本文demo下载

相关推荐
i建模1 天前
在 Windows 中解决 `zig fetch` 的 `TlsInitializationFailed` 错误
windows·zig
问道飞鱼1 天前
【Tauri框架学习】Windows 11 环境下 Tauri 开发环境安装与问题解决手册
windows·学习·tauri·开发环境
xiaoliuliu123451 天前
Autodesk官方卸载工具使用教程(Windows版,含解压+管理员运行+批量卸载)
windows
johnrui1 天前
集合与树形结构
开发语言·windows
柯儿的天空1 天前
【OpenClaw 全面解析:从零到精通】第 006 篇:OpenClaw 在 Windows/WSL2 上的安装与部署实战
人工智能·windows·语言模型·chatgpt·ai作画
阿昭L1 天前
说说Windows进程的令牌(token)
windows·系统安全·token
包饭厅咸鱼1 天前
小龙虾openclaw----Windows+Wsl+Docker 安装openclaw 并接入飞书
windows·docker·openclaw·小龙虾
※※冰馨※※1 天前
【QT】TortoiseGit配 SSH 克隆 Codeup
开发语言·c++·windows
今夕资源网1 天前
坚果手机直连Windows,打开软件实现键鼠操作TNT系统 视频教程+所需软件(今夕存档)
windows·智能手机·tnt·smartisan·smartisan tnt·锤子系统·坚果手机
alphaTao1 天前
LeetCode 每日一题 2026/3/16-2026/3/22
linux·windows·leetcode