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.
Is there anything as device independent pixels in Qt Designer?
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