3

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"))
JeJo
  • 30,635
  • 6
  • 49
  • 88
ImNew
  • 97
  • 9
  • @eyllanesc I get QVector(("name1", "name2", "name3"), ("value1", "value2", "value3"), ("add1", "add2", "add3")) – ImNew Jul 16 '19 at 12:24
  • 1
    @ImNew What is your primary requirement? You have many options to achieve the same. `QVector` is another option. But you have mentioned about `QList` in the question! – JeJo Jul 16 '19 at 12:26

2 Answers2

2

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]});
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
0

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"
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97