Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

Do you have to set text properties in you .ui files? Usually when you set the text property of a widget then UIC replaces completely the Font of that widget with something that it creates from scratch in code. If you edit them on windows then the font will have a windows related name which might cause problems on the mac.

What I usually do is not to touch the fonts in the designer so that the widgets get their defaults fonts which usually look fine and change them in the c'tor of the widget like so:

QFont f = ui.someLabel->font(); // get the current (default) font from the widget
f.setBold(true); // change only what's need to be changed
ui.someLabel->setFont(f); 
      // set the new and impreved font back to where it came from

This way you can avoid messing with anything that is platform specific. If your change is actually platform specific you can choose the right one with the use of #ifdef Q_OS_WIN32 or #ifdef Q_OS_MAC

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top