QMap :: contains () does not return the expected value

I have a class containing a QMap object:

QMap<QString, Connection*> users;

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

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 does not contain () return true?

+3
source share
1 answer

In Unicode, two lines can be displayed the same way, but in reality they are different. Suppose that in the case when you want to normalize strings first:

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

Of course, you will need to normalize the line before you place it on your users map.

+4
source

Source: https://habr.com/ru/post/1770772/


All Articles