如何以及在何处保存 Android 上的 QSettings?

How and where do I save QSettings on Android?

我开始为我的程序制作一个设置表单,但我有点受困于设置数据。我可以制作一个 QSettings 实例,但我应该用它做什么?

QApplication app(argc, argv);
QSettings settings;
settings.setValue("test", QVariant((int)42));
// Now what?

通常情况下,您不需要知道设置的存储位置。 QSettings 的常规使用:您需要设置您的组织名称以及您的应用程序的名称。保存时需要设置section和key,还有一个参数。

//set names
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setApplicationName("Star Runner");
//...
QSettings settings;
//saving
settings.setValue("MySection/MyKey", 42);
//loading: section, key, and default value (default value will be used if the setting doesn't exist)
int val = settings.value("MySection/MyKey", 0).toInt();

更新

//Perhaps, for Android it is necessary to call a function
settings.sync();

(来自文档:

void QSettings::sync() 

Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application. This function is called automatically from QSettings's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself

)