[Qt-interest] Organizing error message strings

Andre Somers andre at familiesomers.nl
Thu Apr 8 09:02:12 CEST 2010


Hi,

On 7-4-2010 21:48, John Weeks wrote:
> Thank you, André.
>
> On Apr 6, 2010, at 11:21 PM, André Somers wrote:
>
>    
>> Interesting question.
>> IMHO, using a QMap (or better yet, a QHash) would be a reasonable
>> approach. I think you’ll just have to try if the initialization time
>> of a suitably large QMap or QHash would take too much time for you.
>> My guess would be that it is not an issue. You can speed up
>> initialization a bit by calling QHash::reserve. Why don’t you create
>> a small test to see how much time it actually takes to initialize a
>> QHash like that with 10,000 messages or so? A simple loop would give
>> you a rough estimate.
>>      
> Hm. Good point. So I wrote a class to wrap the error messages that
> uses a QMap<int, QString>. The constructor looks like this:
>
> ErrorStrings::ErrorStrings()
> {
> 	messageMap.insert(1, tr("out of memory"));
> 	messageMap.insert(2, tr("expected wave name"));
> 	messageMap.insert(3, tr("syntax error"));
> 	messageMap.insert(4, tr("expected comma"));
> 	messageMap.insert(5, tr("expected name of active axis"));
>
> etc.
>
> There are almost 1319 strings. Using QTime to get the time required to
> run the constructor returns 1 mS, which is the resolution of
> QTime::elapsed(). So I guess my fears on that score were groundless!
>
> QMap has no reserve() function. I suppose that's because it's based on
> a list structure rather than an array.
>    
I don't know too much about the internal structures, but I do know that 
QHash has a reserve() function. The only reason to prefer QMap AFAIK, is 
if you need to be able to retreive the items in the order you put them 
in. Otherwise, I'd advise you to use a QHash. Note that if you use 
consequitive error numbers like in your example, you may as well use a 
simple QList or QVector. Lookup will be even faster, and memory 
consumption is lower.


> I'm still wondering if this is the Qt Way. Or maybe my application is
> unusual?
>    
I guess not a lot of people are *that* structured about errormessages, 
even if they should be. That includes me. Especially the requirement to 
be able to retreive an errorstring with a specific function strikes me 
as unusual. I usually define the error where it occurs. But that does 
not work with your requirements. As far as I'm aware, there is not 
really a Qt-way to handle errors.

André




More information about the Qt-interest-old mailing list