views:

62

answers:

3

We have a text-analysis application where the text being analyzed can be of any culture (English, Spanish, Thai, Arabic, etc.) Different cultures have different rules for strings: sorting, word breaks, determining which characters are the same, etc.

How do I make Qt container classes culture-aware? For example, if I am using QMap<QString, int>, I want the keys to stay sorted based on the rules of a user-specified culture. (The culture is not necessary the same as the host OS.) Likewise, it should consider "duplicates" according to the rules of that culture.

In .Net, all such containers can be constructed with a culture-aware comparer such as IEqualityComparer<TKey>. Is there an equivalent in Qt? If not, what workarounds exist?

+1  A: 

QString does have localeAwareCompare for that purpose. QMap does not take a compare function as template parameter but I think you can use STL map for that.

Stephen Chu
I don't think you can specify a particular locale for the localeAwareCompare (it uses the system locale). Plus I need it to be case-insensitive. Looks like the STL map might come to the rescue, combined with some ICU locale algorithms.
Dave
+1  A: 

I don't think there is a simple way to do this with a QMap. It is possible using std::map which has a constructor that lets you specify a custom comparator.

Job
+2  A: 

As has been pointed out, you could use std::map, but another option is to create a LocalizedString class that has a QString conversion operator. If you then overload less than and friends so that they defer to localeAwareCompare() or a comparison object of your own creation, I think you'll end up with pretty much what you need.

Kaleb Pederson