QFont.toString的格式是否在Qt版本之间记录和/或稳定?

问题描述

QFont.toString

The documentation表示返回“以逗号分隔的属性列表”,但未指定“ the”属性是什么或它们出现的顺序。我发现an old question的答案表明这些属性是:

字体系列,pointSizef,pixelSize,QFont::StyleHintQFont::WeightQFont::Style,下划线,strikeOut,fixedPitch,rawMode

但是我找不到有关此信息的权威来源(而且该答案已有10多年历史了,并且关于Qt4)。

是否有QFont.toString()格式的明确文档,包括其代表的属性以及它们的顺序?假设保存这样的字符串,然后在其他版本的Qt上与QFont.fromString()一起使用,是否合理?

解决方法

没有文档说明哪些属性及其序列化顺序。通常,Qt不会指示Qt类的序列化顺序,因为它们可以变化,但是我认为在QFont的情况下,它应该建立一个明确的顺序,因此我建议将其报告为bug。因此,知道顺序的唯一方法是检查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;
}

我认为,如果格式更改,则Qt将实现逻辑,以便以与QDataStream相似的方式支持所有格式,因此通常您不必担心。