views:

371

answers:

2

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.

+1  A: 

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

shoosh
Thanks for your answer. Well, that's a way of dealing with elements which are not rich-formated. But that's a shame QtCreator does not handle it platform-independently.
moala
+2  A: 

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"&gt; <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!

BastiBense
Well, you explained much of the problem; still, that does not resolve the overkill of all that. Thanks anyway for the time spent over this.
moala
Which overkill? Actually if your problem is the one I described above, there is no other way that I know of to fix it. Qt Creator/Designer simply has a nasty behavior that places font size definitions all over the HTML it produces. The only way to get around fonts resulting in having different sizes on different platforms is to remove those font size definitions from the HTML. That's what I do in my own applications.
BastiBense
Then one lose the WYSIApproxWYG advantage of the editor.
moala
Yeah. But often the fact that "it works" is more important than the fact that "it was easy to do". I'm not saying that this is a convenient way of working around this problem, but you were asking for a solution of your problem as described above. Here it is.
BastiBense
It was so hard to figure this out, but I got rid of the styling by right-clicking my QLabels and using "Edit plain text", and also by removing any custom font styling on the Labels/Buttons by clicking the reset button (swooping arrow). Worked a charm!
Rob