Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

c++ - Style sheets / Qt Designer support for high dpi screens?

Today I have ported my application from Qt5.5 to Qt5.6RC. Running it on my high dpi screen the widgets appeared tiny. After reading this and setting QT_AUTO_SCREEN_SCALE_FACTOR to "1" at least it is usable again.

However they say:

In the longer term, the application should be adapted to run unmodified:

1) Always use the qreal versions of the QPainter drawing API.
2) Size windows and dialogs in relation to the screen size.
3) Replace hard-coded sizes in layouts and drawing code by values calculated from font metrics or screen size.

Not all style sheet attributes support em (".. from font metrics"). No idea how I would use "screen size" relative in style sheets. Also Qt Designer only supports px in many places, like shown below.

icon size

Considered I do not want to give up designer and stylesheets, what are my options to create genuine Qt hires applications?


Related (but no answer to my question)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...