Solved [Solved]Appending to Last added QString in QStringList
-
Hello All,
As my previous post this might also be a silly question, but I am processing a data and creating QStringList as its output,
but at certain point I want to append to same index of QStringList multiple times.
PseudoCode:QStringList mylist;
for(i = 0; i < 35; i++)
{
mylist.append("this is ");
if(condition)
{
mylist.append("the output");
}
}So I want "This is the output" as single string appended to same location in QStringList.
Is this possible?Thanks!
-
@Rututraj said in Appending to Last added QString in QStringList:
So I want "This is the output" as single string appended to same location in QStringList.
Is this possible?
Thanks!Why not just use
QStringList::last()
?QStringList mylist; for(i = 0; i < 35; i++) { mylist.append(QString("this is")); if(condition) mylist.last().append(" the output"); }
-
the obvious solution would be to check the condition before inserting (as it's inside your loop)
QStringList mylist; for(i = 0; i < 35; i++) { QString str("this is"); if(condition) mylist.append(str); else mylist.append(str + QString("the output")); }
alternatively:
QStringList mylist; for(i = 0; i < 35; i++) { mylist.append(QString("this is")); if(condition) mylist[mylist.size()-1] = mylist.last() + QString("the output"); }
-
@Rututraj said in Appending to Last added QString in QStringList:
So I want "This is the output" as single string appended to same location in QStringList.
Is this possible?
Thanks!Why not just use
QStringList::last()
?QStringList mylist; for(i = 0; i < 35; i++) { mylist.append(QString("this is")); if(condition) mylist.last().append(" the output"); }
-
@KroMignon
I could have sworn last() is a const reference only, but apparently it also has a pure reference overload 🤷♂️ -
@J-Hilk Other way around: const T &QList::last() const is an overload of T &QList::last(). I was surprised as well.
-
Just a side note in all the answers above where
QString(const char*)
is used, it should be replaced withQStringLiteral
(see docs for the explanation) -
@KroMignon This works fine, Thanks :)