Changing DPI scaling size of display make Qt application's font size get rendered bigger

Arun picture Arun · Dec 9, 2013 · Viewed 45.2k times · Source

I have created some GUI application using Qt. My GUI application contains controls like push button and radio button. When I run the application, buttons and fonts inside button looks normal. When I change the DPI scaling size of display from 100% to 150% or 200%, font size of controls rendered bigger but not control size (pushbutton, radio button) irrespective of resolution. Due to this the text inside controls were cut off. please see the attached image.

Qt application look when DPI scaling size set to 100%

Qt application look when DPI scaling size set to 100%

Qt application look when DPI scaling size set to 200%

Qt application look when DPI scaling size set to 200%

I am running my application in some tablets also. In tablets, DPI scale value should be more than 150% else everything will be shown very small.

I searched in the web for creating UI application in Qt irrespective of resolution and DPI scale value but no luck. So I am posting my questing here. Please let me know if there is some way to get rid of this.

Answer

Nicolas Holthaus picture Nicolas Holthaus · Mar 17, 2016

High DPI support is enabled from Qt 5.6 onward.

Setting QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling) in your application source code allows automatic high-DPI scaling.

NOTICE: To use the attribute method, you must set the attribute before you create your QApplication object:

#include <QApplication>

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

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