Designing UIs with QtCreator under Windows, and porting the same .ui
file under MacOSX leads to designs with some text parts very small -- actually, the HTML ones. It seems it comes from the fact that QtCreator uses pt
instead of px
as text size unit, and that the default screen resolutions are quite different under Windows and MacOSX.
Is there any reason I didn't come to more consistent results? Apart from editing each pt
into px
, are there any workaround?
Thanks.
Simply use the setFont() method on the QApplication or QWidget : QFont font("Courier New"); font. setStyleHint(QFont::Monospace); QApplication::setFont(font);
Default Application Font QT has a default font. That default font is different on different platforms. On Windows it is "Arial" (a sanserif style) but on Linux it is a serifed style similar to Times Roman. If your application is going to run on different platforms you should define "Lato" as the default font.
As a rule of thumb you should not specify the font sizes for controls manually in Qt Designer/Creator as this leads to the prolems you have. The reason for inconsistency is the fact that different platforms use different DPI settings (96 dpi on Windows vs. 72 DPI on Mac OS X). This results in fonts being displayed with different sizes.
Also, you mentioned HTML. I assume you have set some HTML text in a QTextEdit
-like widget using the built-in editor. When you select a font size there, Qt Creator will produce some HTML like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Sans'; font-size:11pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Hello World</p></body></html>
As you can see, it sets some font-size
attributes, which is really nasty. A simple, easy solution to this desaster is to remove the style=
attributes entirely. This causes the QTextEdit
to use the default application font instead (which should be fine on all platforms):
<html><head></head><body><p>Hello World</p></body></html>
As a sidenote, this is much friendlier for translators, as they don't have to fight through all the useless CSS.
Unfortunately Qt's QTextEdit does not support the "percent" font-size specification (just px and pt). If it did, you could have used something like "90%" to make the text smaller than the default font while still being on the safe side.
Another option would be a QWebView, which you make editable. This allows for good text formatting while having the full CSS subset. But that might be overkill.
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With