Hello World< ;/p>
正如你所看到的,它设置了一些 font-size
属性,这确实很讨厌。解决这个灾难的一个简单、容易的解决方案是完全删除 style=
属性。这会导致 QTextEdit
使用默认的应用程序字体(在所有平台上都应该没问题):
p>Hello World
顺便说一句,这对翻译人员来说更加友好,因为他们不必费尽心思地处理所有无用的 CSS。
不幸的是,Qt 的 QTextEdit 不支持“百分比”字体大小规范(仅 px 和 pt)。如果是这样,您可以使用“90%”之类的东西来使文本小于默认字体,同时仍然安全。
另一个选择是 QWebView,您可以将其设置为可编辑。这样可以在拥有完整 CSS 子集的同时实现良好的文本格式设置。但这可能有点矫枉过正了。
希望有帮助!
您是否必须在 .ui
文件中设置文本属性?通常,当您设置小部件的文本属性时,UIC 会用它在代码中从头开始创建的内容完全替换该小部件的字体。如果您在 Windows 上编辑它们,则字体将具有与 Windows 相关的名称,这可能会在 Mac 上引起问题。
我通常做的是不要触摸设计器中的字体,以便小部件获得通常看起来不错的默认字体,并在小部件的 c'tor 中更改它们,如下所示:
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
这样您就可以避免弄乱平台的任何内容具体的。
如果您的更改实际上是特定于平台的,您可以使用 #ifdef Q_OS_WIN32
或 #ifdef Q_OS_MAC
选择正确的更改
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
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 thestyle=
attributes entirely. This causes theQTextEdit
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!