Is the format of QFont.toString() documented and/or stable across Qt versions?

877 Views Asked by At

The documentation for QFont.toString says that it returns "a comma-separated list of the attributes", but it doesn't specify what "the" attributes are or what order they come in. (Edit: the documentation has been updated so it now explicitly lists the attributes and their order.) I found an old question whose answer says that the attributes are:

font family, pointSizeF, pixelSize, QFont::StyleHint, QFont::Weight, QFont::Style, underline, strikeOut, fixedPitch, rawMode

But I can't find any authoritative source for this information (and also that answer is more than 10 years old and is about Qt4).

Is there any explicit documentation of the format of QFont.toString(), including which attributes it represents and what order they come in? Is it reasonable to assume that saving such a string and later using it with QFont.fromString() on a different version of Qt will work?

2

There are 2 best solutions below

0
On BEST ANSWER

There is no documentation that indicates which attributes and the order in which they are serialized. Generally Qt does not indicate the order of how the Qt classes are serialized since they can vary but I think that in the case of QFont it should establish an explicit order so I recommend reporting it as a bug. Therefore, the only way to know the order is to check the source code:

// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/text/qfont.cpp?h=5.15#n2070
QString QFont::toString() const
{
    const QChar comma(QLatin1Char(','));
    QString fontDescription = family() + comma +
        QString::number(     pointSizeF()) + comma +
        QString::number(      pixelSize()) + comma +
        QString::number((int) styleHint()) + comma +
        QString::number(         weight()) + comma +
        QString::number((int)     style()) + comma +
        QString::number((int) underline()) + comma +
        QString::number((int) strikeOut()) + comma +
        QString::number((int)fixedPitch()) + comma +
        QString::number((int)   false);

    QString fontStyle = styleName();
    if (!fontStyle.isEmpty())
        fontDescription += comma + fontStyle;

    return fontDescription;
}

I think that if the format changes then Qt will implement the logic so that all formats are supported in a similar way as it does with QDataStream so in general you should not worry about it.

1
On

Late but ... Inside the Qt6 QFont doc to method toString() the string content is described: [https://doc.qt.io/qtforpython/PySide6/QtGui/QFont.html?highlight=qfont#PySide6.QtGui.PySide6.QtGui.QFont.toString]

However, it seems that when returning a QFont from QFontDialog (Python 3.9.4, Qt 6.2.2 via PySide6, OSX Big Sur) 2 additional parameters are provided before the last parameter ("Font style (omitted when unavailable)")

Also, there is only 1 PointSize value and PointSizeF seems no longer available.

Unfortunately I did not find the latest sources online ....