The location of data warehouses for specific applications varies across platforms. Qt 5 provides a smart solution through QStandardPaths .
Typically, you save the settings for each user in QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) . If you want the settings not to be saved in the user roaming profile on Windows, you can use QStandardPaths::AppLocalDataLocation , it has the value AppDataLocation on platforms other than Windows.
Before you can use the standard paths, you must provide your application name through QCoreApplication::setApplicationName and the name of your organization using setOrganizationName or setOrganizationDomain . The path will depend on them, so make sure they are unique to you. If you ever change them, you will lose access to the old settings, so make sure that you stick to the name and domain, which makes sense to you.
The path is not guaranteed. If this is not the case, you should create it yourself, for example. using QDir::mkpath .
int main(int argc, char ** argv) { QApplication app{argc, argv}; app.setOrganizationDomain("stackoverflow.com"); app.setApplicationName("Q32525196.A32535544"); auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); if (path.isEmpty()) qFatal("Cannot determine settings storage location"); QDir d{path}; if (d.mkpath(d.absolutePath()) && QDir::setCurrentPath(d.absolutePath())) { qDebug() << "settings in" << QDir::currentPath(); QFile f{"settings.txt"}; if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) f.write("Hello, World"); } }
source share