문제

I have read a string in from an XML file with UTF8 encoding. I store this value in a QString object and concatenate it with some other information. I can see in the QtCreator debugger that the QString holds the desired value.

I need to pass this string to another library that takes a const char * argument. How do I obtain the const char * please? I've tried toLocal8bit(), toLatin1(), toUtf8() all with no luck. Seems like I am missing something...

도움이 되었습니까?

해결책

I need to pass this string to another library that takes a const char * argument. How do I obtain the const char * please? I've tried toLocal8bit(), toLatin1(), toUtf8() all with no luck. Seems like I am missing something...

Because if you try those, you only get a QByteArray back as opposed to a const char*. You could use the constData() method on the QByteArray you got that way, and then you will have the desired result.

Mixing std::string and QString is not necessary, hence it is better to go through the qt types (QString -> QByteArray -> const char*) without introducing std::string in here.

Where you use toLocal8bit(), toLatin1() or toUtf8() depends on your encoding, so that is a different question to the main one in here. They will all return a QByteArray to you.

Note: you should avoid using toAscii() though as that is deprecated in Qt 5 for good.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top