0

我正在使用 QSettings 编写一个 .ini 文件,该文件将充当我的应用程序配置文件。只有一个问题:

QSettings 将组名 [General] 添加到文件顶部。

我这样设置文件:

QSettings settings(QApplication::applicationDirPath() + fileName, QSettings::IniFormat);

并像这样写:

settings.setValue("some_setting", theNumber);

但是,我最终将此数据提供给的程序无法处理 [General] 标签。虽然从文件中手动删除 [General] 当然是我的一个选项,但我想知道是否可以让 QSettings 停止这样做。

我怀疑这种行为是由于指定QSettings::IniFormat. 但是,我没有看到任何其他选项会告诉它停止指定组。

知道我该怎么做吗?

4

2 回答 2

4

.ini 文件格式是基于 Microsoft 从 Windows 实施的事实上的标准。它需要部分的存在。

无论您将文件提供给哪个应用程序,都不会真正实现.ini文件格式,而是实现其他东西。

您很可能根本不应该使用QSettings,而是手动实现该功能。这应该不是问题,因为您这样做只是为了将数据“提供”给其他一些软件。你不需要读回来。您可以将设置存储在常规QSettings中,然后将它们导出到文本文件以供该应用程序阅读。

于 2014-08-14T23:28:07.237 回答
3

It's time to write your own QSettings file format.

"file format" is a pair of two metods with signatures:

bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map);
bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map);

one should fill map from device, second should dump map to device. You can use there anything you want -- xml, plain text files without [general] group etc.

After it, you should register your new shiny format with QSettings::registerFormat function.

Here is documentation: http://qt-project.org/doc/qt-5/qsettings.html#registerFormat

good luck.

于 2014-08-15T15:35:13.040 回答