0

Qt 5.7 is claiming to have improved high DPI support. With modern Qt it is possible to create an app starter like:

#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. At least it doesn't scale the UI for me under Linux. 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.

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.

It seems to be the same for the QMessageBox static methods too. Display a static QMessageBox, like QMessageBox::info and its text and icon do not get scaled up to compensate for the high 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.

I've gotten some traction setting the application's style sheet to use larger font for 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 . ( setenv (3) could work under Linux, I guess )

Alexander V
  • 8,351
  • 4
  • 38
  • 47
infixed
  • 1,155
  • 7
  • 15
  • No automatic UI scaling available for Linux yet due to OS not supporting that, mind that certain custom Linux subsystems e.g. GNOME do that but there is no common interface for Qt to take advantage of. – Alexander V Oct 19 '16 at 00:42
  • @AlexanderVx I was sort of thinking that one should be able to have something in main() on the order of `if ( QGuiApplication::primaryScreen()->physicalDotsPerInch() > N ) QSomethingOrOtherClass::setGlobalScaleFactor(2)`. And do it without OS support. Its actually what I thought setting `Qt::AA_EnableHighDpiScaling` as an attribute would do. – infixed Oct 19 '16 at 12:46

1 Answers1

2

As of Qt5.11 the following seems to be good enough for my Ubuntu 18.04 laptop with a 4k screen:

  • Download and install Qt5.11 from official website (not from apt).
  • Open the ~/.local/share/applications/DigiaQt-qtcreator-community.desktop file.
  • 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
  • Restart Qt Creator.

enter image description here

qed
  • 22,298
  • 21
  • 125
  • 196