My Qt application should be able to create/open/save a single "Project" at once. What is the painless way to store project's settings in a file? Should it be XML or something less horrible?
我的Qt应用程序应该能够立即创建/打开/保存单个“项目”。将项目设置存储在文件中的无痛方法是什么?它应该是XML还是不那么可怕的东西?
Of course data to be stored in a file is a subject to change over time.
当然,要存储在文件中的数据是随时间变化的主题。
What I need is something like QSettings
but bounded to a project in my application rather than to the whole application.
我需要的是QSettings之类的东西,但是我的应用程序中的项目而不是整个应用程序。
3 个解决方案
#1
You can use QSettings to store data in a specific .ini file. From the docs:
您可以使用QSettings将数据存储在特定的.ini文件中。来自文档:
Sometimes you do want to access settings stored in a specific file or registry path. On all platforms, if you want to read an INI file directly, you can use the QSettings constructor that takes a file name as first argument and pass QSettings::IniFormat as second argument. For example: QSettings settings("/home/petra/misc/myapp.ini", QSettings::IniFormat);
有时,您确实希望访问存储在特定文件或注册表路径中的设置。在所有平台上,如果要直接读取INI文件,可以使用QSettings构造函数,该构造函数将文件名作为第一个参数,并将QSettings :: IniFormat作为第二个参数传递。例如:QSettings设置(“/ home / petra / misc / myapp.ini”,QSettings :: IniFormat);
#2
I order to make it user editable, I would stick to plain text with one key = values by line, like in most of the Linux apps.
我命令用户可以编辑,我会坚持使用一个键=一行的原始文本,就像在大多数Linux应用程序中一样。
However this is only for the settings, not for the complete project's data which I suppose requires more complex structures.
然而,这仅适用于设置,而不是整个项目的数据,我认为这些数据需要更复杂的结构。
So maybe JSON ?
也许是JSON?
#3
Pro XML:
- You can have a look at it in an editor
- You can store any kind of string in any language in it (unicode support)
- It's simple to learn
- More than one program can read the same XML
- It's easy to structure your data with XML. When you use key/value lists, for example, you'll run into problems when you need to save tree-like structures.
您可以在编辑器中查看它
您可以使用任何语言存储任何类型的字符串(unicode支持)
这很容易学习
多个程序可以读取相同的XML
使用XML构建数据很容易。例如,当您使用键/值列表时,当您需要保存树状结构时,您将遇到问题。
Contra XML
- The result is somewhat bloated
- Most programming languages (especially old ones like C++) have no good support for XML. The old XML APIs were designed in such a way that they could be implemented in any language (smallest common denominator). 'nuff said.
- You need to understand the concept of "encoding" (or "charset"). While this may look trivial at first glance, there are some hidden issues which can bite you. So always test your code with some umlauts and even kanji (Japanese characters) to make sure you got it right.
结果有些臃肿
大多数编程语言(特别是像C ++这样的旧编程语言)对XML没有很好的支持。旧的XML API以这样的方式设计,即它们可以用任何语言(最小的公分母)实现。 '努夫说。
您需要理解“编码”(或“charset”)的概念。虽然乍一看这看起来微不足道,但有一些隐藏的问题会让你感到困惑。所以总是用一些变音符号甚至汉字(日语字符)来测试你的代码,以确保你做对了。
#1
You can use QSettings to store data in a specific .ini file. From the docs:
您可以使用QSettings将数据存储在特定的.ini文件中。来自文档:
Sometimes you do want to access settings stored in a specific file or registry path. On all platforms, if you want to read an INI file directly, you can use the QSettings constructor that takes a file name as first argument and pass QSettings::IniFormat as second argument. For example: QSettings settings("/home/petra/misc/myapp.ini", QSettings::IniFormat);
有时,您确实希望访问存储在特定文件或注册表路径中的设置。在所有平台上,如果要直接读取INI文件,可以使用QSettings构造函数,该构造函数将文件名作为第一个参数,并将QSettings :: IniFormat作为第二个参数传递。例如:QSettings设置(“/ home / petra / misc / myapp.ini”,QSettings :: IniFormat);
#2
I order to make it user editable, I would stick to plain text with one key = values by line, like in most of the Linux apps.
我命令用户可以编辑,我会坚持使用一个键=一行的原始文本,就像在大多数Linux应用程序中一样。
However this is only for the settings, not for the complete project's data which I suppose requires more complex structures.
然而,这仅适用于设置,而不是整个项目的数据,我认为这些数据需要更复杂的结构。
So maybe JSON ?
也许是JSON?
#3
Pro XML:
- You can have a look at it in an editor
- You can store any kind of string in any language in it (unicode support)
- It's simple to learn
- More than one program can read the same XML
- It's easy to structure your data with XML. When you use key/value lists, for example, you'll run into problems when you need to save tree-like structures.
您可以在编辑器中查看它
您可以使用任何语言存储任何类型的字符串(unicode支持)
这很容易学习
多个程序可以读取相同的XML
使用XML构建数据很容易。例如,当您使用键/值列表时,当您需要保存树状结构时,您将遇到问题。
Contra XML
- The result is somewhat bloated
- Most programming languages (especially old ones like C++) have no good support for XML. The old XML APIs were designed in such a way that they could be implemented in any language (smallest common denominator). 'nuff said.
- You need to understand the concept of "encoding" (or "charset"). While this may look trivial at first glance, there are some hidden issues which can bite you. So always test your code with some umlauts and even kanji (Japanese characters) to make sure you got it right.
结果有些臃肿
大多数编程语言(特别是像C ++这样的旧编程语言)对XML没有很好的支持。旧的XML API以这样的方式设计,即它们可以用任何语言(最小的公分母)实现。 '努夫说。
您需要理解“编码”(或“charset”)的概念。虽然乍一看这看起来微不足道,但有一些隐藏的问题会让你感到困惑。所以总是用一些变音符号甚至汉字(日语字符)来测试你的代码,以确保你做对了。