Qt程序中设置屏幕自动缩放因子:根据分辨率调整UI
这串代码需要放在程序的入口处执行,即在main函数的开始部分执行。这样可以确保在程序运行之前就设置好了屏幕的自动缩放因子。\n\n例如,你可以将代码添加到main函数的开头部分:\n\n```cpp\n#include
int main(int argc, char *argv[]) { QApplication a(argc, argv);
// 获取当前屏幕的分辨率
QScreen *screen = QGuiApplication::primaryScreen();
int width = screen->size().width();
int height = screen->size().height();
// 设置自动缩放因子
if (width == 1920 && height == 1080) {
qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", QByteArray("1.5"));
} else if (width == 3840 && height == 2160) {
qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", QByteArray("1"));
}
return a.exec();
}
原文地址: https://www.cveoy.top/t/topic/pZbT 著作权归作者所有。请勿转载和采集!