Style sheets / Qt Designer support for high dpi screens?

六月ゝ 毕业季﹏ 提交于 2019-11-28 20:43:49
Rakeeb Hossain

If you're really sold on using the Designer, perhaps consider updating Stylesheets programmatically. This is a really hacky fix I implemented in an app I had to deploy across multiple platforms. This would mean using px measurements for everything, as it is the only widely supported measurement.

As you may know, Qt fonts do not support 'screen-size' relative em units:

From Qt5 documentation: https://doc.qt.io/qt-5/stylesheet-reference.html Qt is limited to font sizes in pt and px and any other size must be in px, em or ex.

Whichever monitor and its specifications you've developed a UI you are happy with will become the standard for your application. In the constructor of your windows, you can calculate a scale factor as the new device's DPI divided by the 'standard DPI' that you know your application looks good on. From there, you simply would need to multiply your widgets/fonts stylesheets by that scale factor. It may sound cumbersome (and it definitely is), but this would mean iterating through the QString returned by ui->myWidget->getStyleSheet() and scaling all number instances preceding a px substring.

This worked for me in scaling up/down fonts and widget sizes, but with obviously added overhead. I too also really like the simplicity of using the Designer but, afaik, creating scalable UI's remains difficult in Designer. Making the switch to programmatically designing UI's ended up replacing my hacky fix for my application.

Note: It is also important to look out for what fonts you're using. If your font is not a System-supported font, the px value will have unexpected behaviour, since a default font will replace it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!