How to add items from multiple QStringLists to one?

2.5k Views Asked by At

If I have several QStringLists, for example:

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

Is there any way to get list of lists (nested list) like QStringList listAll; that will look like this:

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

There are 2 best solutions below

0
On BEST ANSWER

From the comment, it looks like you are trying pack the list of strings to a QVector instead of QList. In that case, do simply iterate through the QStringList s and append the list of strings created from the equal indexes to the vector.

#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]});
}
0
On

you dont need a vector for that, keep using the 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"