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.
qt-interest-bounces@xxxxxxxxxxxxx [mailto:qt-interest-bounces@xxxxxxxxxxxxx] Namens
Verzonden: dinsdag 6 april 2010 18:34
Onderwerp: [Qt-interest] Organizing error message strings
I'm in the process of porting a very large application to
Qt. There are thousands of error messages, and I'm trying to figure out the Qt
way to organize such a large number of strings.
1) to be able to localize the messages, so it will involve
using tr(), clearly.
2) to retrieve the messages based on an error code.
QString msg = GetErrorMessage(errorcode)
3) to make it *easy* for developers to add new messages. If
it's not easy, then they won't do it, and we get messages like "Syntax
error" because they just use something that's already there.
I've thought about using something like
QMap<int, QString> messageMap
messageMap.insert(oneErrorCode, tr("You made a mistake"))
It would make look-up easy and fast, but it seems like this
approach requires a lot of initialization at start-up. The problem, obviously,
is entering and storing the strings in a way that makes for easy look-up.
What have others done to solve this problem? Is there a
"Qt Way" to do this?