[Development] qPrintable encoding issues

Thiago Macieira thiago.macieira at intel.com
Fri Feb 21 21:55:56 CET 2014


Em sex 21 fev 2014, às 10:48:18, Matthew Woehlke escreveu:
> On 2014-02-21 04:20, Koehne Kai wrote:
> > This is a heads up that you should avoid using qPrintable() together
> > with qDebug/QDebug for localized strings.
> > 
> > The issue is that qPrintable() does '.toLocal8Bit()', while
> > QDebug/qDebug and friends nowadays expect const char * arguments to
> > be UTF-8 encoded.
> 
> When you say "nowadays", has something changed? (In particular, is the
> above true of Qt 4.x also?)

It did in Qt 5.0, when we decided that all source code would be encoded in 
UTF-8. THAT modification doesn't affect Qt 4.x.

However, the problem was already present there:
            case 's': {
                if (length_mod == lm_l) {
                    const ushort *buff = va_arg(ap, const ushort*);
                    const ushort *ch = buff;
                    while (*ch != 0)
                        ++ch;
                    subst.setUtf16(buff, ch - buff);
                } else
                    subst = QString::fromUtf8(va_arg(ap, const char*));
                if (precision != -1)
                    subst.truncate(precision);
                ++c;
                break;
            }

It already expected UTF-8 data. The only reason why this isn't a problem is 
that qDebug() is most definitely not 8-bit clean in Qt 4.x. So the encoding of 
the source is string does not matter, it's going to get garbled anyway.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Development mailing list