Convert pixel size to point size for fonts on multiple platforms

情到浓时终转凉″ 提交于 2021-01-21 10:31:29

问题


I need a way to translate point size and pixel size between multiple platforms.

I have a Qt application that must run on multi-platform, including an embedded Linux on a type of tablet.
It is expected that users can save files created by the application, on a desktop (either windows or linux) and open on the custom device.

The data consists of drawings, and text - QGraphicsItems on a QGraphicsScene. Some text items are "rich text", so we can change font on fragments of text.

For normal text, including all UI text, we used pixel size instead of point size to achieve a similar look. But rich text defies me: the QTextCharFormat doesn't have a pixelSize() option. Only setFontPointSize() and fontPointSize(). I can use font().setPixelSize() then setFont() but the result is that when saving, using the html() method, I lose all font information. (Must be a qt bug ?)

So, what I need is to be able to use pixel size everywhere, and then calculate the point size to set it on paragraphs (and go in reverse when reading sizes).

But - what is the relation between pixel size and point size ? If I determine both, for a given font, on the current platform, can I establish some sort of equation to use ?

Edit - I found an interesting post - it seems to do what I want, but it is specific to only OSX. https://stackoverflow.com/a/25929628/1217150
My target platforms, Windows / Linux / OSX but also, especially, a custom tablet running embedded Linux, and possibly in the future Android devices.

Qt 4.8

Edit - using the conversion in answer below, left text using setPixelSize(20) and right text using setPointSize(20 * screenDpi) where

qreal screenDpi = QApplication::desktop()->physicalDpiX() / 72.;

Note the size is not the same... (running in windows, have not yet tested on other platforms)

I even tried

#ifdef Q_OS_WIN32
    qreal screenDpi = QApplication::desktop()->physicalDpiX() / 96.;
#else
    qreal screenDpi = QApplication::desktop()->physicalDpiX() / 72.;
#endif


回答1:


Yes, I think it is possible:

double ptToPx(double pt, double dpi) {
    return pt/72*dpi
}

double pxToPt(double px, double dpi) {
    return px*72/dpi
}

...

double dpi = QGuiApplication::primaryScreen()->physicalDotsPerInch();
qDebug() << "12 pt is" << ptToPx(12, dpi) << "px";
qDebug() << "26 px is" << pxToPt(26, dpi) << "pt";



回答2:


But rich text defies me: the QTextCharFormat doesn't have a pixelSize() option. Only setFontPointSize() and fontPointSize().

You can set prepared QFont with QTextCharFormat::FontPropertiesSpecifiedOnly behavior parameter to set only pixelSize:

QFont font;
font.setPixelSize(18);
QTextCharFormat fmt;
fmt.setFont(font, QTextCharFormat::FontPropertiesSpecifiedOnly);


来源:https://stackoverflow.com/questions/42141354/convert-pixel-size-to-point-size-for-fonts-on-multiple-platforms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!