4

I have a 4k monitor and I've installed Qt 5.6 RC + VS 2015 + Qt5Package. Everything is set up, I can design and compile my applications developed previously on normal DPI PC and it runs perfectly.

But...

When I first opened layout.ui file of my project on highdpi screen, I saw that the main window was sized 1000 x 500 px. The window in Qt Designer shows in highdpi, its sharp and it corresponds to what you get when app is compiled. But when you change size of the window in Qt Designer, or of any element, it is using absolute pixels on your screen. So a form wide half of your screen width in QtDesigner translates to 2000px wide window. This compiles and runs ok on highdpi machine, but when app is executed on standard size screen, its enormous.

This affects all elements that are absolutely sized.

Problem is, that every thread online solves issues when designing on low DPI and running on high dpi. This can be done by QT_DEVICE_PIXEL_RATIO. But it will not work reverse, whatever absolute size you set on highDPI will look huge on standard DPI screen.

  1. Is there anything as device independent pixels in Qt Designer?

  2. What approach do you suggest to solve this? Say I need 20px on 96dpi screen paddings, but this becomes about 50 px on 240dpi screen. Do I have to set EACH such dimension from C++ code?

I am now using this trick inside main function:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyApp w;

    // I designed this on 240DPI screen 3840x2160px
    qreal refDpi = 240.;
    qreal refWidth = 3840.;
    qreal refHeight = 2160.;

    // gets screen resolution
    QRect rect = a.primaryScreen()->geometry();
    qreal width = qMax(rect.width(), rect.height());
    qreal height = qMin(rect.width(), rect.height());

    // gets DPI
    qreal dpi = a.primaryScreen()->logicalDotsPerInch();

    // get ratio of my 240 DPI and current DPI
    qreal m_ratio = qMin(height / refHeight, width / refWidth);
    qreal m_ratioFont = qMin(height*refDpi / (dpi*refHeight), width*refDpi / (dpi*refWidth));

    // resize accordingly
    w.resize(w.width() * m_ratio, w.height() * m_ratio);

    // this is just a little tweak not to allow the window to grow over window size. likely unnecessary
    w.setMaximumSize(width, height);

    w.show();

    return a.exec();
}

But I can't / don't want to do this for every single dimension, as there are tons of UI elements and some have width, some have padding etc. The whole idea of separating UI and program logic would go in vain.

Thanks

michnovka
  • 2,880
  • 3
  • 26
  • 58
  • The upcoming Qt release will provide support for high dpi displays. Currently there is no support, so no way to get it automatically in the designer. In QML code it is quite easy to fake by using some varying and customizable unit instead of pixels directly. – dtech Feb 27 '16 at 15:28
  • I am using QT 5.6 RC and this feature is not present. I thought official release should only fix bugs, not bring new features. – michnovka Feb 29 '16 at 18:23
  • Well I don't know in that case. I never use the designer, IMO it sucks. Typing is the way to go :) – dtech Feb 29 '16 at 18:32

0 Answers0