tags:

views:

57

answers:

1

I have a class that contains a QMap object:

QMap<QString, Connection*> users;

Now, in the following function Foo(), the if clause always returns false but when I iterate through the map, the compared QString, i.e., str1 is present in the keys.

void Foo(QString& str1, QString& str2)
{    
    if(users.contains(str1))
        users[str1]->doStuff(str2);
    else
    {
        for(QMap<QString, Connection>::iterator iter = users.begin(); 
                           iter!= users.end();iter++)
            qDebug()<<iter.key();
    }
}

Am I doing something wrong? Why doesn't contains() return true ?

+2  A: 

With unicode, two strings make be rendered the same but actually be different. Assuming that's the case you'll want to normalize the strings first:

str = str.normalize(QString::NormalizationForm_D);
if (users.contains(str))
    // do something useful

Of course, you'll need to normalize the string before you put it in your users map as well.

Kaleb Pederson
Even after normalizing the QString before storing in the QMap and before checking, contains() returns false only.
Saurabh Manchanda
Please show us in what way the strings are different. For example, `qDebug() << str.toUtf8()` should show us the actual bytes that create the string.
Kaleb Pederson
After cleaning the project and rebuilding, it worked. Thanks.
Saurabh Manchanda