2

Using Qt5.6.1, I am using QtWinMigrate to house a Qt widget in a parent window. When the parent app is scaled for a high-dpi monitor, the following line:

pApp = new QApplication( argc, argv );

kills the scaling, resizing the parent window so that 150% or 200% scaling apparently jumps down to 100%. This is before I've even created my Qt window - just the constructor of QApplication does this. I've tried various tricks. A qt.conf file like this:

[Platforms]
WindowsArguments = dpiawareness=0,1,2

or before creating the QApplication:

QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setDesktopSettingsAware(true);

None of it seems to have an effect. How can I prevent this resizing?

Roderick
  • 1,205
  • 11
  • 24

1 Answers1

2

In qt.conf we supposed to use one value for DPI Awareness setting:

[Platforms]
WindowsArguments = dpiawareness=2
# either 0 or 1 or 2

If you want to use this dpiawareness of qt.conf setting and prevent the Qt GUI from being automatically scaled, use value 2 which is Per Monitor DPI Aware and not handled by Qt. "Per-monitor DPI aware" used for allowing the system to send an event down to the window while being dragged from one monitor to another. It is just like the GUI app will behave with natural API, either handling or not handling that event. The rest of UI scaling is just disabled in this case and but either DPI Unaware (0) or System DPI Aware (1) will do the scaling. Qt High DPI Displays article describes these modes but apparently not sufficiently clear so I am telling you from my own experience.

You can also not use either qt.conf at all or not use dpiawareness attribute and that will also not scale the GUI.

Talking about QApplication constructor, it does watch the app DPI settings. The common pattern for the Qt app starter it:

#include "mywidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    // it is usually on the stack
    QApplication a(argc, argv);
    MyWidget w;
    w.show();

    return a.exec();
}

.. so you don't have to do new QApplication( argc, argv ) or to allocate application object on the heap at all.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • As it happens, I allocate QApplication on the heap because, as I mentioned, this is a hosted Dll in another application, and the initialization function returns to the parent after creating the QApplication. Other than making the QApplication global, which would be problematic in a number of ways, allocating it the stack would cause it to be deleted after initialization. – Roderick Dec 30 '16 at 14:58
  • @Roderick, you can still allocate the app object on the stack as long as the event loop will be running in that thread but you need to make sure that thread quits the loop before destroyed. – Alexander V Dec 30 '16 at 15:49
  • that's not how this particular implementation works, for reasons too numerous and off-topic to discuss here. It's not really germane, the issue is getting Qt to recognize the configuration setting. – Roderick Dec 31 '16 at 00:17
  • @Roderick I can post my own hack that makes Qt app to use certain DPI Awareness mode without reading qt.conf. That way is Qt version specific, though. – Alexander V Dec 31 '16 at 01:46