1

In this simple code I have trouble with Unicode:

QString qs = QFileDialog::getOpenFileName(0,"","","");
std::string str = qs.toUtf8().constData();
Mat m = imread(str);

When qs is Latin char set it works fine, but when path contains Cyrillic chars I obtain a bad conversion. As sample: qs "E:/Кирилиця_49.png": str "E:/Кирилиця_49.png"

I know that happens when str is not on UTF-8 characters set, but in project properties the "Character Set" property is "Use Unicode Character Set". Compiler MSVC 2010, Qt 5.3.2. What could be the problem?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
Bleach
  • 156
  • 3
  • 15
  • `QString` keeps data in unicode. So your conversion is OK. It doesn't depent on "Use Unicode Character Set" property at all. What is your question? Could you say, where exactly do you have a problem? In outputting to debug utf strings? – Dmitry Sazonov Apr 15 '15 at 10:54
  • The problem is in str I receive "E:/Кирилиця_49.png", but not "E:/Кирилиця_49.png". This values I copies from debuger – Bleach Apr 15 '15 at 11:09
  • Why not to use `QString::toStdString()` or rather `QString::toStdWString()`? – vahancho Apr 15 '15 at 11:24
  • QString::toStdString() return bad chars too. toStdWString() works correct, but after I must convert it to std::string. and this can create a problem, because I use of C ++ 99 and not 11 and program runs under Linux, Windows and Android. – Bleach Apr 15 '15 at 11:30

1 Answers1

1

I believe QString::toUtf8() is doing its job right (modulo some bug in Qt's implementation...).

The problem could be that you are using a debugger visualizer for the content of std::string that is showing the string interpreting it using some "code page" instead of Unicode UTF-8.

Basically, the string content (as raw bytes) is correct (it's just the UTF-8 byte sequence corresponding to the original Unicode UTF-16 string): you are just using some "wrong-colored glasses" to look at it :)

The important point is: in what format does the imread() function expect its string parameter to be? If imread() expects an UTF-8 string, then you are right in passing a std::string with UTF-8 encoded string to it as argument.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • > you are just using some "wrong-colored glasses" to look at it :) – Bleach Apr 15 '15 at 11:37
  • 2
    Also to see correct string in debugger `str.c_str (), s8` could be used in case of msvc. – Predelnik Apr 15 '15 at 11:41
  • > you are just using some "wrong-colored glasses" to look at it :) - no because imread(str) return NULL if path or file name contains Cyrillic chars. I think that codepage of std::string is not utf8. How can I change it to utf8? In qt4 I make QtextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8")), but in qt5 this function was obsolete. – Bleach Apr 15 '15 at 11:47
  • > str.c_str (), s8 shows correct string. Mr.C64 was right. And now next question: why imread not work correctly with utf8 string? – Bleach Apr 15 '15 at 11:55
  • http://stackoverflow.com/questions/24769623/opencv-imread-on-windows-for-non-ascii-file-names - the core of the my problem – Bleach Apr 15 '15 at 12:05