简体   繁体   English

如何在Qt中以跨平台的方式获取设置存储路径?

[英]How to get a settings storage path in a cross-platform way in Qt?

My program needs to read/write a text (just a few lines) file with its settings to disk. 我的程序需要读取/写入文本(只有几行)文件及其设置到磁盘。 To specify a path in code may work well on one platform, like windows, but if runs it on Linux, the path is not cross platform. 在代码中指定路径可能在一个平台上运行良好,如Windows,但如果在Linux上运行它,则路径不是跨平台的。

I am looking for a similar solution to QSettings that saves settings to different paths or has its native ways to handle this. 我正在寻找一个类似的QSettings解决方案, QSettings设置保存到不同的路径,或者有其本机方式来处理这个问题。 Programmers don't need to do with the details. 程序员不需要处理细节。 But the text file is not suitable to be saved as a value in QSettings . 但是文本文件不适合在QSettings保存为值。

No user interaction should be needed to obtain such path. 不需要用户交互来获得这样的路径。 The text file should persist across application restarts. 文本文件应该在应用程序重新启动时保持不变。 It can't be a temporary file. 它不能是临时文件。

Is there an existing solution in Qt and what is the API that should be used? Qt中是否存在现有解决方案,应该使用哪种API?

The location of application-specific settings storage differs across platforms. 特定于应用程序的设置存储的位置因平台而异。 Qt 5 provides a sensible solution via QStandardPaths . Qt 5通过QStandardPaths提供了一个明智的解决方案。

Generally, you'd store per-user settings in QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) . 通常,您将在QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)存储每用户设置。 If you wish the settings not to persist in the user's roaming profile on Windows, you can use QStandardPaths::AppLocalDataLocation , it has the meaning of AppDataLocation on non-Windows platforms. 如果您希望设置不在Windows上的用户漫游配置文件中保留,则可以使用QStandardPaths::AppLocalDataLocation ,它在非Windows平台上具有AppDataLocation的含义。

Before you can use the standard paths, you must set your application name via QCoreApplication::setApplicationName , and your organization's name using setOrganizationName or setOrganizationDomain . 在使用标准路径之前,必须通过QCoreApplication::setApplicationName设置应用程序名称,并使用setOrganizationNamesetOrganizationDomain组织的名称。 The path will depend on these, so make sure they are unique for you. 路径将取决于这些,因此请确保它们对您而言是独一无二的。 If you ever change them, you'll lose access to old settings, so make sure you stick with name and domain that makes sense for you. 如果您更改了它们,您将无法访问旧设置,因此请确保您坚持使用对您有意义的名称和域名。

The path is not guaranteed to exist. 路径不保证存在。 If it doesn't, you must create it yourself, eg using QDir::mkpath . 如果没有,则必须自己创建,例如使用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");   
  }
}

如果要保存一些用户相关数据,可以使用QDir::homePath()获取用户主目录路径。

There is QDir to handle paths to dirs, QFileInfo for platform independent file information and QDir 's homePath() QDir来处理dirs的路径, QFileInfo用于平台无关的文件信息和QDirhomePath()

My proposal is to use these classes and use QDir::home() or QDir::homePath() to find a directory where to write to, since the user has write permissions in his homedir and it exists on each platform. 我的建议是使用这些类并使用QDir::home()QDir::homePath()来查找要写入的目录,因为用户在其homedir中具有写权限并且它存在于每个平台上。

You can store file in application directory. 您可以将文件存储在应用程序目录中 Take a look at QCoreApplication::applicationDirPath() . 看看QCoreApplication::applicationDirPath()

From Qt documentation: 从Qt文档:

Returns the directory that contains the application executable. 返回包含应用程序可执行文件的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM