QSettings ini format
-
Hello,
Going thru QSettings, i have 1 particular question regarding the documentation.
https://doc.qt.io/qt-6/qsettings.html#fallback-mechanism paragraph, sentence
Note that INI files lose the distinction between numeric data and the strings used to encode them, so values written as numbers shall be read back as QString. The numeric value can be recovered using QString::toInt(), QString::toDouble() and related functions.
then QVariant's documentation of
toInt
(ortoDouble
for that matter) method contains the following:
Returns the variant as an int if the variant has userType() QMetaType::Int, QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::Double, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.
And doing a small test
#include <iostream> #include "QString" #include "QSettings" int main(int argc, char **argv) { std::cout << "hello world!" << std::endl; QSettings settings{ QSettings::IniFormat, QSettings::UserScope, "Test", "Test" }; auto val = settings.value("key1", 10); if (!val.isNull()) { std::cout << val.toInt() << std::endl; } else { std::cout << "setting key1 = 100!" << std::endl; settings.setValue("key1", 100); } return 0; }
running this, i indeed observe getting numeric value back just fine. With what is written about QSettings and IniFormat, i would expect to have to use this:
QSettings settings{...}; auto value_as_int = settings.value("key").toString().toInt();
However it seems not to be necessary - my question is if this paragraph about
iniFormat
should be dropped and it is safe to directly convert to numeric types with QSettings independent of the format used.Thanks!
VK
-
Hi and welcome to devnet,
That's a good question, I would rather bring it to the interest mailing list. You'll find there Qt developers/maintainers. This forum is more user oriented.