MacOSX 下 Qt 应用程序文本大小不正确

发布于 2024-08-17 00:34:11 字数 331 浏览 12 评论 0原文

在 Windows 下使用 QtCreator 设计 UI,并在 MacOSX 下移植相同的 .ui 文件会导致一些文本部分非常小的设计 - 实际上是 HTML 部分。看来这是由于QtCreator使用pt而不是px作为文本大小单位,并且默认屏幕分辨率在Windows 和 MacOSX。

我没有得到更一致的结果有什么原因吗?除了将每个pt编辑为px之外,还有什么解决方法吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や三分注定 2024-08-24 00:34:11

根据经验,您不应在 Qt Designer/Creator 中手动指定控件的字体大小,因为这会导致出现问题。不一致的原因是不同平台使用不同的 DPI 设置(Windows 上为 96 dpi,Mac OS X 上为 72 DPI)。
这会导致字体以不同的大小显示。

另外,您提到了 HTML。我假设您已经使用内置编辑器在类似 QTextEdit 的小部件中设置了一些 HTML 文本。当您在那里选择字体大小时,Qt Creator 将生成一些如下 HTML:


Hello World< ;/p>

正如你所看到的,它设置了一些 font-size 属性,这确实很讨厌。解决这个灾难的一个简单、容易的解决方案是完全删除 style= 属性。这会导致 QTextEdit 使用默认的应用程序字体(在所有平台上都应该没问题):

p>Hello World

顺便说一句,这对翻译人员来说更加友好,因为他们不必费尽心思地处理所有无用的 CSS。

不幸的是,Qt 的 QTextEdit 不支持“百分比”字体大小规范(仅 px 和 pt)。如果是这样,您可以使用“90%”之类的东西来使文本小于默认字体,同时仍然安全。

另一个选择是 QWebView,您可以将其设置为可编辑。这样可以在拥有完整 CSS 子集的同时实现良好的文本格式设置。但这可能有点矫枉过正了。

希望有帮助!

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!

二货你真萌 2024-08-24 00:34:11

您是否必须在 .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 选择正确的更改

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文