How to get the way to store settings in a cross-platform form in Qt?

My program should read / write text (just a few lines) with its settings on disk. To indicate the path in the code, it can work well on one platform, such as Windows, but if it runs on Linux, the path is not a cross platform.

I am looking for a similar solution for QSettings that saves settings for different paths or has its own ways to handle this. Programmers do not need to deal with details. But the text file is not suitable for saving as a value in QSettings .

To get this path does not require user interaction. The text file should be saved in all application restarts. It cannot be a temporary file.

Is there an existing solution in Qt and which API should I use?

+5
source share
4 answers

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"); } } 
+8
source

If you want to save some user-related data, you can get the path to the user's home directory using QDir::homePath() .

-1
source

There is QDir for handling dirs paths, QFileInfo for independent platform information and QDir homePath()

My suggestion is to use these classes and use QDir::home() or QDir::homePath() to find the directory where to write, since the user has write permissions to his homedir and exists on every platform.

-1
source

You can save the file in the application directory. Take a look at QCoreApplication::applicationDirPath() .

From the Qt documentation:

Returns the directory containing the application executable.

-1
source

All Articles