PDA

View Full Version : QTextCodec::setCodecForCStrings() in libraries



conexion2000
10th August 2007, 21:56
Hi!
Is it good to use QTextCodec::setCodecForCStrings() in external libraries? Will it affect the main application? How can I inform library about used encoding in source? Will using this function in main application affect external libraries? It is difficult to understand, because it's static function.

marcel
10th August 2007, 21:59
It will affect all (const char*) to QString and the other way around conversions from the moment it is called.

Pretty clear.

Regards

conexion2000
10th August 2007, 22:04
Even those that are located in dll's? And Qt libs itself?

How can I inform library itself about used encoding?

marcel
10th August 2007, 22:08
Yes, everything.
If you set a code for C strings and call a function located in a dll, like this(assuming the function takes a QString parameter):


dllFunction("String with weird encoding");
Then the QString built from the const char* will be affected by the codec.

If you don't want this behavior then you could define sections in your applications in which you set the code to NULL, do some stuff, and set it back to utf8.

EDIT: you can make a global variable and/or function in your lib that you use as some sort of initializer.
For example:


setLibraryCodec(currentCodec);


Regards

conexion2000
10th August 2007, 22:18
I rather meant internal functions in a library. Assuming returnString() belongs to SomeLib which is compiled as DLL:

QString SomeLib::returnString()
{
//do some stuff
QString someString("here it goes..");
//do here something
}
Will be some string encoded in a codec set in main app?

marcel
10th August 2007, 22:21
Yes, if you call this function in the context of the application, then I believe it will.
It is better to test it, since I could be wrong :).

However, it does not make any sense not to apply the codec since QString has a static QTextCode member which is set by setCodecForCStrings.
Therefore every QString built after this member is set will be affected.

Regards