If you can use C ++ 11 (qt5 qmake project file: CONFIG += c++11 ), try this short snippet:
QStringList somelist; // fill list for(auto& str : somelist) str = str.trimmed();
It will go through the list with a link, and the result of calling the trimmed function will be returned to the element in the original list.
Without using C ++ 11, you can use Qt methods with Java-Style mutable by iterators:
QMutableListIterator<QString> it(somelist); while (it.hasNext()) { it.next(); it.value() = it.value().trimmed(); }
The last method is just perfect if, for example, you want to delete empty lines after they have been trimmed:
QMutableListIterator<QString> it(somelist); while (it.hasNext()) { it.next(); it.value() = it.value().trimmed(); if (it.value().length() == 0) it.remove(); }
Remedy really, see Java-style Qt iterator docs
source share