0

In Qt 4.8 this was working fine.

QString test = "this is a test message for special characters ‘sk’ @#$%^&*()"; qDebug()<<"test message"; qDebug()<< test;

But in QT 5.9 I am getting a REPLACEMENT CHARACTER .

Output in Qt 4.8

test message this is a test message for special characters ‘sk’ @#$%^&*()

But in Qt 5.9 I am getting this output

test message this is a test message for special characters �sk� @#$%^&*()

What is the problem with Qt 5.9? what are the fixes available for this?

sk110
  • 77
  • 2
  • 9
  • Hm, `‘` and `'` are different characters. What do you expect? – vahancho Sep 29 '17 at 09:33
  • I just want to print message in single quotes like I was doing in previous version. Is that possible in Qt 5.9 – sk110 Sep 29 '17 at 10:25
  • Are you sure the replacement is in the `QString`? I think maybe it's just that `QDebug` can't handle those characters: they are not ASCII. – strubbly Sep 29 '17 at 10:49
  • I tried it in debug mode and by setting it as text to QLabel still the same result. So I don't think it's qDebug issue. On converting the string to QByteArray it shows ‘ as \xef\xbf\xbd and in this link http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65280&utf8=string-literal it is given as REPLACEMENT CHARACTER. – sk110 Sep 29 '17 at 12:27
  • Note that a major change between Qt 4 and 5 is the encoding that the `QString(const char *)` constructor assumes. In Qt 5 it **must** be UTF-8. In Qt 4 the local encoding was used. Reencode your source files to UTF-8, or use `u8` string literals to produce UTF-8 sequences out of your input charset. – peppe Sep 29 '17 at 21:16

1 Answers1

1

With 8bit character string constants they are often converted to ascii by the compiler so even while the source code may allow certain characters that doesn't mean it will work. Just did a quick test with Qt 5.9 and Visual Studio 2017. Same problem you describe but when you force the 8-bit string to be utf8 with the u8 prefix it works.

QString test = u8"this is a test message for special characters ‘sk’ @#$%^&*()"; 
qDebug()<<"test message"; 
qDebug()<< test;
Eelke
  • 20,897
  • 4
  • 50
  • 76