PDA

View Full Version : QString contains fails to find comma



NicholasSmith
6th August 2010, 14:14
Hi guys, I reckon I'm missing something fairly simple but I'm baffled at what. I'm passing a function a QList<QString> array of a load of info pulled out of a database, then processing it down into a CSV file with some tweaks. All works well up until it hits anything with a comma in it, the issue now is my code doesn't seem to find it.


if(passedList.value(i).contains(","))
{
qDebug() << "replacing";
passedList.value(i).replace(",", " ");
}

That's basically what I've been using, but it never seems to be tripped, and I'm wondering if I'm using .contains wrong in that method.

Any help would be appreciated!

Lykurg
6th August 2010, 14:40
It does not reach the scope or is just not replaced. The later is because value returns a copy, so you need to use QList::at().

tbscope
6th August 2010, 14:45
Or use a QStringlist and use replaceInStrings(...)

NicholasSmith
6th August 2010, 14:57
Thanks for the quick responses.

Lykurg: I tried with both .value(i) and .at(i) and neither trigger when the condition is met. Unless you mean in a manner different to passedList.at(i).contains.

tbscope: that's my other option, I wasn't aware of it when I first wrote my function and it'd involve a bit of refactoring to get it changed. I'll probably go down that route when I give up trying to get the .contains working. Intellectual challenge, or working out why I'm being a bit slow!

Lykurg
6th August 2010, 15:03
I see no problem with your syntax. so make a debug version an look at the values:


qDebug() << passedList;
qDebug() << i;
qDebug() << passedList.value(i);
qDebug() << passedList.value(i).contains(",");
if(passedList.value(i).contains(","))
{
qDebug() << "replacing";
passedList.value(i).replace(",", " ");
}What does it returns?

tbscope
6th August 2010, 15:10
at() and value() are const
You can't directly change a list like this, try this:


#include <QtCore/QCoreApplication>

#include <QStringList>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

qDebug() << "Test replacing comma in a string list";
qDebug() << "";

QStringList list;
list << "a, b, c" << "d, e, f";

for (int c = 0; c < list.count(); ++c) {
qDebug() << "Line" << c+1 << "=" << list.at(c);
}

qDebug() << "";
qDebug() << "And now without a comma:";

for (int c = 0; c < list.count(); ++c) {
QString line = list.at(c);
line.replace(',', ' ');
list.replace(c, line);
qDebug() << "Line" << c+1 << "=" << list.at(c);
}

return a.exec();
}

It gives this output:

Test replacing comma in a string list

Line 1 = "a, b, c"
Line 2 = "d, e, f"

And now without a comma:
Line 1 = "a b c"
Line 2 = "d e f"

NicholasSmith
6th August 2010, 15:14
Ah hah, it is actually spotting it and triggering, it just doesn't seem to be replacing now.

Lykurg
6th August 2010, 15:22
at() and value() are constAh, I messed it up. Once again. at() is returning a const reference, but value returning a value (which is not const for the returning value, this it what I meant). I will never keep that in mind...
const T & at ( int i ) const
T value ( int i ) const

EDIT: Without a temporary QString you have to use the [] operator.

NicholasSmith
6th August 2010, 15:27
Thanks chaps! I decided to just go down the temporary string route, and it works perfectly now.