Question

Is there any neat way, short of converting number to QByteArray, to save quint64 with QSettings? The problem is QVariant doesn't accept qint64 nor quint64.

Était-ce utile?

La solution

QVariant supports qlonglong and qulonglong. As the documentation says, these are the same as qint64 and quint64. So you can just use QVariant::QVariant(qlonglong) and QVariant::toLongLong.

Autres conseils

What if you store qint64 as a string. QString supports such conversion: QString::number(qlonglong n, int base), where qlonglong is the same as qint64. The same for quint64 - use QString::number(qulonglong n, int base), where qulonglong is the same as quint64.

QSettings settings("config.ini", QSettings::IniFormat);
[..]
qint64 largeNumber = Q_INT64_C(932838457459459);
settings.setValue("LargeNumber", QString::number(largeNumber));
[..]

Another solution is to realize that IEEE 754 double format has a 53 bit fraction (don't forget the implicit 53rd bit!) and a sign bit. This allows you to store unsigned 53 bit integers without loss of precision, or signed 54 bit integers. You can store if:

  • your qint64's absolute value is smaller than 2^55, or
  • your quint64 is smaller than 2^54.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top