QT 高DPI解决方案

一、根据DPI实现动态调整控件大小(三种方式)

1、QT支持高DPI(针对整个进程中所有的UI)

// main函数中
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling)

tips:(1)如果不想全局设置,可以使用下面两种方式进行单独设置。(2)如果想有的窗口使用系统高DPI缩放,有的窗口保持DPI,就需要设置该属性,并修改QT源码,增加接口,来判断某个窗口是否需要高DPI自动缩放

2、存在该头文件<QtWidgets/private/qstylehelper_p.h>

#include <QtWidgets/private/qstylehelper_p.h>

qreal KStyle::dpiScaled(qreal value)
{
   
#ifdef Q_OS_MAC
	// On mac the DPI is always 72 so we should not scale it
	return value;
#else
	return QStyleHelper::dpiScaled(value);
#endif
}

3、不存在该头文件<QtWidgets/private/qstylehelper_p.h>,自己实现

#include <QScreen>

qreal KStyle::dpiScaled(qreal value)
{
   
#ifdef Q_OS_MAC
	// On mac the DPI is always 72 so we should not scale it
	return value;
#else
	qreal dpi = QGuiApplication::primaryScreen()->logicalDotsPerInch(); 
	static const qreal scale = qreal(dpi) / 96.0;
	return value * scale;
#endif
}

二、使用示例

ui->btnClose->setFixedHeight(dpiScaled(ui->btnClose->minimumHeight()));

相关推荐

  1. QT DPI解决方案

    2024-01-03 12:42:01       62 阅读
  2. Python 并发解决方案有哪些?

    2024-01-03 12:42:01       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-03 12:42:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-03 12:42:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-03 12:42:01       87 阅读
  4. Python语言-面向对象

    2024-01-03 12:42:01       96 阅读

热门阅读

  1. 分布式(4)

    2024-01-03 12:42:01       55 阅读
  2. 分布式(3)

    2024-01-03 12:42:01       59 阅读
  3. 安卓作业001 - 显示学生信息

    2024-01-03 12:42:01       50 阅读
  4. 单片机相关知识点

    2024-01-03 12:42:01       64 阅读
  5. 提升开发效率,程序员都在使用的免费api

    2024-01-03 12:42:01       67 阅读
  6. 【ASP.NET Core 基础知识】--介绍

    2024-01-03 12:42:01       52 阅读
  7. 静态pod

    2024-01-03 12:42:01       57 阅读
  8. Unity游戏引擎的2D碰撞检测

    2024-01-03 12:42:01       72 阅读
  9. 欢迎来到MySQL优化之旅

    2024-01-03 12:42:01       67 阅读