简体   繁体   English

使用Qt中的自动缩放字体大小设计用户界面

[英]Design user interfaces with automatically scaling fontsizes in Qt

Qt 5.7 is claiming to have improved high DPI support. Qt 5.7声称改进了对高DPI的支持。 With modern Qt it is possible to create an app starter like: 使用现代的Qt,可以创建一个像以下这样的应用启动器:

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);   
    return app.exec();
}

I expect UI to automatically scale when running on high DPI, but the scaling doesn't necessarily work as expected. 我希望UI在高DPI上运行时会自动缩放,但是缩放不一定能按预期工作。 At least it doesn't scale the UI for me under Linux. 至少在Linux下,它不能为我扩展UI。 What I am seeing is that the layout scales up, but the fonts stay right where they were, at the sizes Qt Creator assigned them in the form layout tool. 我看到的是布局会扩大,但是字体会保持原样,即Qt Creator在表单布局工具中为其分配的大小。

If you want a larger size font for some element, and you set it in form Design screen, there seems no way to say "twice as big". 如果要为某个元素使用较大的字体,并在“设计”屏幕上进行设置,似乎没有办法说“两倍大”。 Instead it injects a font property with an absolute point size. 相反,它将注入具有绝对磅值的font属性。

It seems to be the same for the QMessageBox static methods too. QMessageBox静态方法似乎也是如此。 Display a static QMessageBox, like QMessageBox::info and its text and icon do not get scaled up to compensate for the high dpi. 显示一个静态QMessageBox,例如QMessageBox::info ,其文本和图标未按比例放大以补偿高dpi。

So exactly what is it you are supposed to do to allow your Qt application, designed in Creator at a standard DPI, to automatically adjust to a high DPI environment, fonts, QMessageBoxes and all. 因此,您究竟应该怎么做才能使在Creator中以标准DPI设计的Qt应用程序自动适应高DPI环境,字体,QMessageBoxes等。

I've gotten some traction setting the application's style sheet to use larger font for QMessageBox. 我已经设置了应用程序的样式表,以将较大的字体用于QMessageBox。 but it feels ugly, and I'm not sure how to trigger it automatically. 但感觉很丑,我不确定如何自动触发它。

EDIT: 编辑:

Manually setting the environment variable 手动设置环境变量

declare -x QT_SCALE_FACTOR=2

Does seem to invoke the sort of behavior I am looking for. 确实会引起我正在寻找的那种行为。 But how to do it automatically only on High DPI environment, and preferably inside the program itself . 但是如何仅在High DPI环境上自动执行此操作,最好在程序本身内部执行。 ( setenv (3) could work under Linux, I guess ) (我猜setenv (3)可以在Linux下工作)

As of Qt5.11 the following seems to be good enough for my Ubuntu 18.04 laptop with a 4k screen: 从Qt5.11开始,以下内容对于4k屏幕的Ubuntu 18.04笔记本电脑似乎已经足够了:

  • Download and install Qt5.11 from official website (not from apt). 从官方网站(而非apt)下载并安装Qt5.11。
  • Open the ~/.local/share/applications/DigiaQt-qtcreator-community.desktop file. 打开~/.local/share/applications/DigiaQt-qtcreator-community.desktop文件。
  • Change the line Exec=/path/to/Qt/Tools/QtCreator/bin/qtcreator to Exec=env QT_AUTO_SCREEN_SCALE_FACTOR=1 /path/to/Qt/Tools/QtCreator/bin/qtcreator 将行Exec=/path/to/Qt/Tools/QtCreator/bin/qtcreatorExec=env QT_AUTO_SCREEN_SCALE_FACTOR=1 /path/to/Qt/Tools/QtCreator/bin/qtcreator
  • Restart Qt Creator. 重新启动Qt Creator。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM