c++ - How to convert QString in Qt5 to LPCSTR in Visual Studio 2013 -
I got this question (unhealthy in my case): Most of you probably will consider copying, but I think It is related to different QT / VS / C ++ version or there is no correct answer of the bus (despite some it has been marked).
I have this code, which uses Qt5 in Visual Studio 2013 on Windows 7, C + uses all the proposed solutions in the associated answer:
< Code> QString test = "Hello World"; // 1 LPCSTR lp1 = _T (test.toLocal8bit (.) ConstData ()); // 2 LPCSTR lp2 = _T (test.toUtf8 (). ToStdString () .c_str ()); // 3 LPCSTR lp3 = _T (test.toLatin1 (). ToStdString () .c_str ());produces
îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþç'Û} ÆS2¹úººú
produces
îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþFþš
production
îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþFþš
how do I change this?
Thanks to HuntsMan @ irc.freenode.net I found the right solution:
Caststring Test = "Hello World"; QByteArray test_ar = test.toLocal8 bit (); LPCSTR lp2 = _T (test_ar.constData ()); The problem was that a copy of QByteArray was deleted from the stack, as soon as I got out of that line of code, so I passed the Parent LPCRR by pointing out the memory removed. having had.
Comments
Post a Comment