Qt:带有 INI 文件和注释的 Windows 上的 QSettings

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1712500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:25:29  来源:igfitidea点击:

Qt: QSettings on Windows with INI files and comments

windowsqtini

提问by dwj

I have an application I'm writing using Qt 4.5.2 on Windows. I'm storing some settings in an INI file and using QSettings to load and save the settings. I'd like to have some comments in the INI file.

我有一个在 Windows 上使用 Qt 4.5.2 编写的应用程序。我将一些设置存储在 INI 文件中,并使用 QSettings 加载和保存设置。我想在 INI 文件中有一些评论。

For example:

例如:

; Meta-info to store with the file
[General]
MainWindow\size=@Size(1280 600)
MainWindow\pos=@Point(0 300)
Debugging=true

However, I've found when I load the settings file with

但是,我发现当我加载设置文件时

QSettings settings( "settings.ini", QSettings::IniFormat );

the comments are stripped out of the file. The INI file is re-written after loading by a call to QSettings::sync()(this is done automatically by the constructor). Is there a way to preserve the comments after syncing?

注释从文件中删除。INI 文件在加载后通过调用QSettings::sync() 重写(这是由构造函数自动完成的)。有没有办法在同步后保留评论?

Preemptive comments:

抢先评论:

  • I want INI files in Windows for future cross-platform compatibility
  • I want to store meta-info in the file for reference outside of the application
  • I am considering making the meta-info a section of the INI and using the name=valuerules but would prefer to keep the information as a comment
  • 我想要 Windows 中的 INI 文件以实现未来的跨平台兼容性
  • 我想将元信息存储在文件中以供应用程序外部参考
  • 我正在考虑将元信息作为 INI 的一部分并使用name=value规则,但更愿意将信息保留为注释

采纳答案by Gy?rgy Andrasek

QSettingshas no concept of "save". All the changes you do to it is considered to be final, and written to disk often and transparently.

QSettings没有“保存”的概念。您对其所做的所有更改都被认为是最终的,并且经常且透明地写入磁盘。

In the documentation of QSettings, there is no mention about comments in ini files. It does makes some sense: after all, it can be a registry value, too. Treat it like a generated file: it is one.

在 的文档中QSettings,没有提及 ini 文件中的注释。它确实有一定的意义:毕竟,它也可以是一个注册表值。把它当作一个生成的文件来对待:它是一个。

Here's my suggestion:

这是我的建议:

[General]
Comment = @String(Meta-info to store with the file)
MainWindow\size=@Size(1280 600)
MainWindow\pos=@Point(0 300)
Debugging=true

I don't know if it works, play around with it to see how it actually stores the string. Oh, and make sure you either set it from code or document it properly, to avoid accidentally using the same identifier from within the program.

我不知道它是否有效,试试看它实际上是如何存储字符串的。哦,并确保您从代码中设置它或正确记录它,以避免在程序中意外使用相同的标识符。