While working on some modernization of my application — in particular,
converting some UTF-8 literals to use QStringLiteral — I noticed a
concerning compiler warning:

  warning C4566: character represented by universal-character-name
  '\uXXXX' cannot be represented in the current code page (1252)

After doing some testing, it turns out that, given code like
QStringLiteral("\u269E \U0001f387 \u269F"), MSVC is indeed butchering
the string.

Further investigation shows that the problem seems to be with the
implementation of QStringLiteral. In particular, it appears that the
preprocessor initially sees just the raw string literal without the 'u'
prefix, butchers it, then later "promotes" it to a UTF-16 literal, but
by then the damage has been done.

While this absolutely feels like a compiler bug, it's an *awful* big
gotcha that probably should be documented. Also, is there anything that
Qt can do to work around it? (I know these sorts of macro expansions can
be tricksy...)

Note: and the *local* work-around is apparently to include the 'u'
prefix on my own literal; apparently doubling it (`uu"stuff"`) is okay.

-- 
Matthew
#include <QApplication>
#include <QLabel>

int main(int argc, char** argv)
{
  QApplication app{argc, argv};
  QLabel label;
  label.setText(QStringLiteral(u"\u269E \U0001f387 \u269F"));
  label.setAlignment(Qt::AlignCenter);

  auto f = label.font();
  f.setPixelSize(256);
  label.setFont(f);

  label.show();
  return app.exec();
}
_______________________________________________
Development mailing list
Development@qt-project.org
https://lists.qt-project.org/listinfo/development

Reply via email to