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 - QGraphicsItem
s 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
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With