AFAICS here , QStringBuilder does not have a% = operator.
However, if you want to keep your loop, you can try something like this:
#include <QStringBuilder> #include <QStringList> #include <iostream> using namespace std; int main(int argc, char *argv[]) { QStringList words; words << "a1" << "a2" << "a3"; QString testString; for (auto it = words.constBegin(); it != words.constEnd(); ++it) { testString = testString % *it % " "; } cout << testString.toStdString() << endl; }
Also mentioned is the QT_USE_QSTRINGBUILDER macro, which turns all + usage into % , provided that this does not cause problems elsewhere in your code.
EDIT:
In light of Marvin's comment, I think I should add a few clarifications to my answer: This answer shows one way to explicitly use QStringBuilder and the% operator in a loop. QStringBuilder was created to optimize concatenation expressions, and this optimization is achieved by eliminating the need for time series and calculating the total size of the concatenated string and distributing it immediately (obviously, this can be done only at the "end" of the expression).
This means that its optimal use is probably not in a loop (for example, the code above). However, even then it gives you some optimization, as can be seen from the gprof and Measure-Command output for the two versions below.
Version 1 - QStringBuilder and% operator (gprof cumulative seconds: 0.46; PowerShell Measure-Command: 5: 23s)
for (auto it = words.constBegin(); it != words.constEnd(); ++it) { for (int i = 0; i < 100000; ++i) { testString = testString % *it % " "; } }
Version 2 - Qstring and + operator (gprof cumulative seconds: 0.61; PowerShell Measure-Command: 10: 47s)
for (auto it = words.constBegin(); it != words.constEnd(); ++it) { for (int i = 0; i < 100000; ++i) { testString = testString + *it + " "; } }
So, I would say that using QStringBuilder and the% operator probably will not leave you noticeably worse (note that the above values ββare a little distorted if your application does not actually perform thousands of concatenations without any I / O). But, as usual, it is up to you to measure the lead time and decide which is best for you.