简体   繁体   中英

Qt utf8 encoding fails on windows but works good on linux

In project we have a code:

return QString("%1\u00B0%2' %3").
    arg(std::abs(data.longitude) / 60).
    arg(std::abs(data.longitude) % 60).
    arg(data.longitude > 0 ? 'E' : 'W');

This string object is printed in QTableView 's cell. The problem is that it prints degree sign correctly only on linux machines but on windows it prints square. How to fix it?

Qt 4.8, Windows XP. Linux (any distribution).

UPD:

We have tried anything but QString::fromLatin1() only helped a bit: on windows now it correctly prints degree sign while on linux we have extra symbol:

°

So, for this moment we have a "solution" like this:

return QString("%1%2%3' %4").
    arg(std::abs(data.longitude) / 60).
#if defined(Q_OS_WIN)
    arg(QString::fromLatin1("\u00B0")).
#else
    arg("\u00B0").
#endif
    arg(std::abs(data.longitude) % 60).
    arg(data.longitude > 0 ? 'E' : 'W');

This is very ugly but it works. I appreciate any other fix.

It's more or less a happy coincident that the unicode \° (16bit on win 32 on unix) represents a valid 0xb0 for latin1. I would deduce the String from its wchar_t representation like

return QString::fromWString(L"%1\u00B0%2' %3").
    arg(std::abs(data.longitude) / 60).
    arg(std::abs(data.longitude) % 60).
    arg(data.longitude > 0 ? 'E' : 'W');

That should work on both platforms and gives you the freedom to encode chars which do not have an 8bit encoding in latin1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM