Qt QString“键值”替换QHash。

时间:2021-11-27 20:05:40

I have a QString with content that looks something like this:

我有一个QString,它的内容看起来像这样:

"resolution=[imagesize]&quality=[imagequal]".

“resolution =图象尺寸和质量=[imagequal]”。

And I have a QHash<QString, QString> mDefaults container that holds a list with some of my 'key' values. ("imagesize" and "imagequal" are so called 'keys' that I need to replace with values from my hash container)

我有一个QHash mDefaults容器,它包含有我的一些“键”值的列表。(“imagesize”和“imagequal”是所谓的“键”,我需要用哈希容器中的值替换它们) ,>

mDefaults.insert("imagesize", "320x240");
mDefaults.insert("imagequal", "standard");

My goal is to get a string that will look something like this: "resolution=320x240&quality=standard" (Note that "[" and "]" are gone too)

我的目标是得到一个看起来像这样的字符串:“分辨率=320x240&quality=标准”(注意,“[”和“]”也消失了)

Is there any fast/good way to do this kind of string key-value replacement with Qt library?

是否有快速/好的方法来使用Qt库进行这种字符串键值替换?

Thanks.

谢谢。

1 个解决方案

#1


3  

for(QMap::iterator i=mDefaults.begin();i!=mDefaults.end();++i) {
    myString.replace(QString("[%1]").arg(i.key()), i.value());
}

#1


3  

for(QMap::iterator i=mDefaults.begin();i!=mDefaults.end();++i) {
    myString.replace(QString("[%1]").arg(i.key()), i.value());
}