QSettings :: IniFormat values with“,”返回为QStringList [英] QSettings::IniFormat values with "," returned as QStringList

查看:789
本文介绍了QSettings :: IniFormat values with“,”返回为QStringList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QSettings 来解析一个ini文件: QSettings cfg(path,QSettings :: IniFormat);

I am using QSettings to parse an ini file: QSettings cfg(path, QSettings::IniFormat);

当我获得一个值 QVariant qv = cfg.value(title); 包含 QStringList 而不是 QString

When I obtain a value QVariant qv = cfg.value("title"); containing a comma the variant contains a QStringList instead of a QString

title=foo => QString
title=foo,bar => QStringList

如何始终获取字符串,或至少获取原始行( title = foo,bar )?

How can I always get strings, or at least obtain the original line ( title=foo,bar ) ?

推荐答案

,所有这些都显示如下:

You have at least two ways to address this issue, all of them presented below:

title="foo,bar"
title_unquoted=foo,bar



main.cpp



main.cpp

#include <QSettings>
#include <QDebug>

int main()
{
    QSettings settings("test.ini", QSettings::IniFormat);
    // Original issue
    qDebug() << settings.value("title_unquoted");
    // 1st solution: join the strings
    qDebug() << settings.value("title").toStringList().join(',');
    // 2nd solution: use quotes in the ini file
    qDebug() << settings.value("title");
    return 0;
}



main.pro



main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp



构建和运行



Build and Run

qmake && make && ./main



输出



Output

QVariant(QStringList, ("foo", "bar"))
"foo,bar"
QVariant(QString, "foo,bar")

换句话说,对包含特殊字符的字符串使用引号,或者在列表中手动连接字符串。前者是更好的,如果它在你的控制下,因为你通常应该瞄准在字符串中使用特殊字符正确的引用。

In other words, use quotes for strings with special characters or join the strings manually in the list. The former is far better if it is under your control as you usually ought to aim for proper quoting when using "special" characters in strings.

这篇关于QSettings :: IniFormat values with“,”返回为QStringList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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