Qt:避免重建应用程序变换字符串(Qt : Avoid Rebuilding Applications incase strings change)

编程入门 行业动态 更新时间:2024-10-28 04:30:46
Qt:避免重建应用程序变换字符串(Qt : Avoid Rebuilding Applications incase strings change)

我想知道在QT应用程序中存储字符串的选项。 我没有重新构建整个项目或任何文件的主要要求之一是一个字符串发生更改,并且所有字符串都在一个地方。简而言之,我希望将字符串放在一个地方并在Application启动期间提取它们

I wanted to know what my options are for storing strings in a QT application. One of my major requirements in not re-building the entire project or any file in-case one string changes and also have all the strings in one place.In short I would like to have the strings in one place and extract them during Application startup

最满意答案

我已经使用了上述答案中谈到的所有元素。

XML , JSON , QSettings w / Ini文件, tr()

所有人都可以做得很好。 我已经对不同的选项做了一些笔记:

QTranslator

Qt Linguist和tr()标签旨在采用您的库存语言并将其翻译成另一种语言。 在没有Qt语言学家的情况下跟踪英语翻译的多个版本以及修改/发布几乎是不可能的。 Qt Linguist需要“发布”您的翻译并将其从TS文件(翻译源)转换为优化的QM文件。

QM文件格式是本地化应用程序使用的紧凑二进制格式。 它提供极快的翻译查找。

以下是使用翻译文件的内容:

QTranslator translator; translator.load("hellotr_la"); app.installTranslator(&translator);

http://qt-project.org/doc/qt-4.8/qtranslator.html#details

我认为使用QTranslator进行一些字符串更改可能是一个奇怪的用例,除非您用于本地化您的程序。 但是就像文档说的那样,它针对非常快速的字符串替换查找进行了优化。

QXMLStreamReader

流阅读器是“推荐”的方式来访问XML文件,或者至少有更好的支持。 您编写自己的文件来组织它,或者编写代码来生成XML。

<STRING_1>Some string</STRING_1>

这是导航到xml的样子。

QXmlStreamReader xml; ... while (!xml.atEnd()) { xml.readNext(); ... // do processing } if (xml.hasError()) { ... // do error handling }

XML与Json非常相似,但是文件较大,开始和结束标记较长。 那里有很多关于XML的股票读者。 在许多情况下,它也更具人性化,因为很多人都知道HTML并且它们非常相似。

QJsonDocument

Qt 5中的JSON支持看起来非常好。 我还没有用它构建一个项目 它看起来很简单,就访问和设置而言,它看起来就像使用字典或地图或矢量。

更新:您只需将指针传递到QJsonDocument或QJsonObject或QJsonArray因为您要更深入地导航或将更多内容附加到Json文件上。 当你完成后,你可以将它保存为二进制文件,或者作为一个明文,人类可读的文件,并有适当的缩进和一切!

如何在Qt5中创建/读/写JSon文件

对许多人来说,Json似乎正在变成XML的替代品。 我喜欢使用Json来保存和加载角色扮演游戏状态的例子。

http://qt-project.org/doc/qt-5/qtcore-savegame-example.html

QSettings

QSettings是我的最爱之一,因为它已经被支持了很长时间,并且它是应该保存和访问大多数持久性设置的方式。

当我使用它时,为了利用默认值和后退机制,我把它放在我的main.cpp中:

QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Star Runner");

而且因为我有时需要在Windows中手动编辑这些设置,所以我使用的是Ini格式。

QSettings::setDefaultFormat(QSettings::IniFormat); // also in main.cpp

然后,当我部署我的exe,并且我希望加载特定值而不是硬编码默认值时,安装程​​序将主要后备内容放入

C:/ProgramData/MySoft/Star Runner.ini

当程序在运行时保存更改时,它将保存到:

C:/Users/<username>/AppData/Roaming/MySoft/Star Runner.ini

然后在整个程序中,如果我需要设置或设置设置,则需要3行或更少的代码。

// setting the value QSettings s; s.setValue("Strings/string_1", "new string"); // getting the value QString str; QSettings s; str = s.value("Strings/string_1", "default string").toString();

这是你的ini文件的样子:

[Strings] string_1=default string

如果要在部署或运行​​时存储要更改的几个字符串, QSettings就是您的选择。 (或者如果现在选中了复选框,或者您的窗口大小和位置,或者最近的文件列表或其他)。

QSettings已经过优化,并且经过深思熟虑。 ini支持非常棒,除了它有时会重新排序组和键(通常按字母顺序排列),它可能会删除您放入其中的任何注释。 我认为ini评论要么以a开头; 或# 。

希望有所帮助。

I've used all of the elements talked about in above answers.

XML, JSON, QSettings w/ Ini files, tr()

All of them can do it just fine. I've put together some notes on the different options:

QTranslator

Qt Linguist and the tr() tags are designed to take your stock language and translate it into another language. Keeping track of multiple versions of the english translation and modifying/releasing without Qt Linguist is almost impossible. Qt Linguist is required to "release" your translations and convert them from a TS file (translation source) to an optimized QM file.

The QM file format is a compact binary format that is used by the localized application. It provides extremely fast lookups for translations.

Here is what using a translation file looks like:

QTranslator translator; translator.load("hellotr_la"); app.installTranslator(&translator);

http://qt-project.org/doc/qt-4.8/qtranslator.html#details

I think using QTranslator for a few string changes may be a weird use case, unless you are using for localizing your program. But like the docs say, it is optimized for very fast look ups of string replacements.

QXMLStreamReader

The stream reader is "recommended" way to access XML files, or at least with better support. You write your own files for organizing it, or you write code to generate the XML.

<STRING_1>Some string</STRING_1>

Here is what it looks like to navigate into xml.

QXmlStreamReader xml; ... while (!xml.atEnd()) { xml.readNext(); ... // do processing } if (xml.hasError()) { ... // do error handling }

XML is very similar to Json, but with larger files and the start and end tags are longer. There are a lot more stock readers out there for XML. It is also a lot more human readable in many cases because so many people know html and they are very similar.

QJsonDocument

The JSON suppport in Qt 5 looks really good. I haven't built a project with it quite yet It is as easy as it looks, and as far as accessing and setting, it looks just like using a dictionary or a map or a vector.

UPDATE: You just pass around a pointer into your QJsonDocument or your QJsonObject or your QJsonArray as you are navigating deeper or appending more onto your Json file. And when you are done you can save it as a binary file, or as a clear text, human readable file, with proper indentation and everything!

How to create/read/write JSon files in Qt5

Json seems to be turning into the replacement for XML for many people. I like the example of using Json to save and load the state of a role playing game.

http://qt-project.org/doc/qt-5/qtcore-savegame-example.html

QSettings

QSettings is one of my favorites, just because it has been supported for so long, and it is how most persistent settings should be saved and accessed.

When I use it, to take advantage of the defaults and fall back mechanisms, I put this in my main.cpp:

QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Star Runner");

And because I sometimes find a need to edit these setting by hand in windows, I use the Ini format.

QSettings::setDefaultFormat(QSettings::IniFormat); // also in main.cpp

Then when I deploy my exe, and I want to have particular value loaded instead of the hardcoded defaults, the installer drops the main fallback into

C:/ProgramData/MySoft/Star Runner.ini

And when the program saves a change at runtime, it gets saved to:

C:/Users/<username>/AppData/Roaming/MySoft/Star Runner.ini

And then throughout my program if I need to get a setting or set a setting, it takes 3 lines of code or less.

// setting the value QSettings s; s.setValue("Strings/string_1", "new string"); // getting the value QString str; QSettings s; str = s.value("Strings/string_1", "default string").toString();

And here is what your ini file would look like:

[Strings] string_1=default string

QSettings is the way to go if you are storing a few strings you want to change on deployment or at runtime. (or if a checkbox is now checked, or your window size and position, or the recent files list or whatever).

QSettings has been optimized quite a bit and is well thought out. The ini support is awesome, with the exception that it sometimes reorders groups and keys (usually alphabetically), and it may drop any comments you put in it. I think ini comments are either started with a ; or a #.

Hope that helps.

更多推荐

本文发布于:2023-08-04 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   应用程序   Avoid   Qt   Rebuilding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!