PDA

View Full Version : Using setCodecForCStrings(codec) in Constructor



danbetz
30th July 2007, 13:11
Hi,
I want to save utf8 encoded QString's in a class called letter. I set the encoding in the constructor. Unfortunately, this means that the first object has already been initialized with the default encoding when the execution of the constructor reaches this statement. Thus, the first element has always the default (wrong) encoding. This is probably more of a C++ issue but I would nonetheless be very grateful if someone points me to a suitable solution.
I know, it would be better to write the files in latin1 instead of utf8 but yet, I would like to do it this way ... for various reasond.

Thanx in advance
Daniel Betz

marcel
30th July 2007, 13:14
void QTextCodec::setCodeForCStrings is static.
You can call this and set a codec in main.cpp, right after you create the QApplicationObject.

Therefore, besides the QApplication, everything will use that codec.
You could even try setting it before instantiating QApllication, but I'm not sure if it will work.

Regards

danbetz
30th July 2007, 18:03
I reduced the Problem as far as i could. The out commented part for setting the codec doesn't make a different.
When i execute the Program it shows the first letter wrong, and the second right.
I think the reason is that i need to set the codec for the class letter. But when it is done at first in the constructor - it is too late, because the QStrings are already initialized with the default latin1 standard- codec. How can I easily solve this without too much modification to the source as it is now ?

marcel
30th July 2007, 18:38
This is all I could do so far.
I am not even sure it works OK, since it gives a strange char for alpha.

Anyway, it didn't work for you since you were using statics, which actually got initialized before main.

Regards

marcel
30th July 2007, 18:56
It is working now.
The idea is not to instantiate Letter objects before you get to set the codec.

GreekAlphabet should behave like a Letter factory and must be instantiated before all Letter objects.

So I removed the statics and created two(for now) static getters that return alpha and beta.

You could improve it further and do it more general.

Regards

danbetz
30th July 2007, 21:23
Thank you very much - that is exactly what i wanted.