Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the pixel ratio (screen) at runtime?

Tags:

qt

qwidget

Qt does support a pixel ratio (devicePixelRatio) which is different on my various desktops:

  1. ) Desktop w1920 h1080 - ratio: 1
  2. ) Desktop w3840 h2160 with qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1") results in Desktop w1280 h720 hi DPI ratio: 3
  3. ) Desktop w3840 h2160 - ratio: 1 without QT_AUTO_SCREEN_SCALE_FACTOR

I wonder if I can manually adjust this ratio for my application, so for instance using "ratio 2" in example 2 from above.

So is there a way to set this value at runtime?

Based on http://doc.qt.io/qt-5/highdpi.html I did try something like

qputenv("QT_SCALE_FACTOR", QString::number(scaleFactor).toLocal8Bit());

Actually I did expect setting int scaleFactor = 3 would look like number 2 above, but it is not the same. Also 1-4 looks weird.

The following 3 lines look similar to qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1") (based on https://stackoverflow.com/a/45168724/356726 )

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // DPI support
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); //HiDPI pixmaps
qputenv("QT_SCALE_FACTOR", "1");

PS: Follow up of

  • Style sheets / Qt Designer support for high dpi screens?
  • How to interpret QFontMetrics results?
  • Can I set QT_AUTO_SCREEN_SCALE_FACTOR behavior via API?
  • Style sheets / Qt Designer support for high dpi screens?
like image 908
Horst Walter Avatar asked Oct 27 '25 06:10

Horst Walter


1 Answers

This is my current workaround as I was asked. It only works if I do it before I construct the UI, so I call it at the beginning.

Passing values around 1.0 (e.g 0.75) work on high DPI screens. But I cannot change it later.

void CGuiApplication::highDpiScreenSupport(double scaleFactor)
{
    if (scaleFactor < 0)
    {
        qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
    }
    else
    {
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // DPI support
        QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); // HiDPI pixmaps
        const QString sf = QString::number(scaleFactor, 'f', 2);
        qputenv("QT_SCALE_FACTOR", sf.toLatin1());
    }
}
like image 122
Horst Walter Avatar answered Oct 29 '25 09:10

Horst Walter