QSettings不区分字符串和int值 [英] QSettings does not differentiate between string and int values

查看:575
本文介绍了QSettings不区分字符串和int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用文件支持的 QSettings 对象编写和读取字符串和int值。
当我以后尝试从不同的进程读取值时,这些值被读取为字符串,而不是int。

I am writing and reading string and int values using a file-backed QSettings object. When I later try to read the values from a different process, the values are read as strings instead of int.

这是我使用的代码写入值:

This is the code I am using to write values:

QSettings settings("TestQSettings.ini", QSettings::IniFormat);
settings.setValue("AAA",QString("111"));
settings.setValue("BBB",222);

这是创建的文件:

This is the file created:

[General]
AAA=111
BBB=222

这是我用来读取值的代码:

This is the code I am using to read values:

QVariant qvar = settings.value("AAA");
std::cout << "AAA type " << qvar.type() << std::endl;
qvar = settings.value("BBB");
std::cout << "BBB type " << qvar.type() << std::endl;

如果我从同一个进程运行这个代码:

If I run this code from the same process:

AAA type 10
BBB type 2


b $ b

如果我从不同的进程运行此代码:

If I run this code from a different process:

AAA type 10
BBB type 10

我知道可以在读取后转换类型。不幸的是,这个解决方案将需要修改Windows旧的跨平台代码,我不喜欢修改,例如多次调用 RegQueryValueEx()

I know it's possible to convert the types after they have been read. Unfortunately, this solution will require modifying Windows legacy cross-platform code which I prefer not to modify, for example multiple calls to RegQueryValueEx().

是否可以存储和读取字符串和整数的类型信息?

Is it possible to store and read the type information for strings and integers?

例如,字符串将引用和整数不会:

For example, Strings will have quotes "" and integers will not:

[General]
AAA="111"
BBB=222

这个问题出现在Qt 4和Qt 5,在Linux上。

This problem is present on both Qt 4 and Qt 5, on Linux.

推荐答案

结果很简单。

当值写入INI文件时,类型是已知的。
我在SetValue之前附加到值\STRING

When values are written to the INI file, the type is known. I am appending to the value "\"STRING right before SetValue

当从INI文件读回值时。
我验证字符串类型有上述后缀。
如果他们这样做,我把postfix关闭。
如果他们不假设他们是整数而不是字符串。

When values are read back from the INI file. I verify that string types have the above postfix. If they do, I chop the postfix off. If they don't I assume they are integers instead of strings.

像一个魅力!

感谢所有人,特别是@Kuba Ober实际提供的解决方案。

Thanks to you all and especially @Kuba Ober for practically handing out the solution.

这篇关于QSettings不区分字符串和int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆