2

如果我有几个QStringLists,例如:

QStringList str1 = {"name1", "name2", "name3"};
QStringList str2 = {"value1", "value2", "value3"};
QStringList str3 = {"add1", "add2", "add3"};

有没有办法获取列表列表(嵌套列表)QStringList listAll;,如下所示:

(("name1","value1","add1"),("name2","value2","add2"),("name3","value3","add3"))
4

2 回答 2

2

从评论中,您似乎正在尝试将字符串列表打包到 aQVector而不是QList. 在这种情况下,只需遍历QStringLists 并将从相等索引创建的字符串列表附加到向量。

#include <QVector>

QVector<QStringList> allList;
allList.reserve(str1.size()); // reserve the memory

for(int idx{0}; idx < str1.size(); ++idx)
{
    allList.append({str1[idx], str2[idx], str3[idx]});
}
于 2019-07-16T12:21:10.500 回答
0

你不需要一个向量,继续使用 StringList

QStringList str1={"name1", "name2", "name3"};
QStringList str2={"value1", "value2", "value3"};
QStringList str3={"add1", "add2", "add3"};

QStringList r{};
// use elegantly the << operator and the join method instead... 
r << str1.join(",") << str2.join(",") << str3.join(",");
qDebug() << "result : " << r.join(";");

//result:
//"name1,name2,name3;value1,value2,value3;add1,add2,add3"
于 2019-07-16T14:13:06.063 回答