[Development] qPrintable encoding issues

Koehne Kai Kai.Koehne at digia.com
Fri Feb 21 10:20:15 CET 2014


Hi,

This is a heads up that you should avoid using qPrintable() together with qDebug/QDebug for localized strings. E.g.

qWarning("%s", qPrintable(tr("Localized error")));

is bound to break on some setups (e.g. Russian windows versions).

The issue is that qPrintable() does '.toLocal8Bit()', while QDebug/qDebug and friends nowadays expect const char * arguments to be UTF-8 encoded. This shouldn't be an issue if you stick to ASCII, but you should avoid using translated strings in this setup.

I'm not sure we can really do anything but document this. Changing qPrintable might break other legitimate use cases (e.g. writing to a file, printf()...). Changing qDebug/QDebug would break our promise that we expect all source code / literals to be UTF-8 encoded ...

As I said, most uses of qPrintable() should be fine, since we typically do not translate debug messages. But we have a few cases inside Qt where we actually write localized strings via qDebug (e.g. Qml error messages, console.log output) ...

Other ideas how to fix this are of course welcome :)

Regards

Kai




More information about the Development mailing list