How to convert QString to LPCSTR (Unicode)

我的未来我决定 提交于 2019-12-06 07:47:14

I guess:

QString str("ddddd");
LPCSTR lstr = str.toStdString().c_str();

QString can always hold Unicode; LPCSTR is never Unicode. This means that you do have to consider what to do with the characters that won't fit. This isn't a "which method to use" question, but a design question.

It's quite possible that in your specific case, you absolutely know that the QString only contaisn characters from your local "ANSI" codepage (also known as ACP). In that case, the correct function is QString::toLocal8Bit ().

Alternatively, you might know that the QString only contains characters from Latin1 (ISO 8859-1). In that case, the correct function is QString::toLatin1().

You could try to call QString::toUtf8(). This will always produce a valid byte array, even if the QString contained all Unicode characters. However, formally you can't point a LPCSTR to it: UTF-8 is not a valid ACP codepage. And presumably, you want this LPCSTR to pass to another function outside your control. It's likely that function won't expect UTF-8. If it expected Unicode at all, it would take a LPCWSTR.

I found following solution from here and it works flawlessly for me:

void fooSub(LPSTSTR X); // this is our function :-)

foo()
{
    QString text;
    if(sizeof(TCHAR) == 1)
        fooSub((LPCSTR)text.toLocal8Bit().constData()); // here you have to check, how to convert, you could also use utf8(), ...
    else
        fooSub((LPCWSTR)text.utf16());
}
LPCSTR == const char *

it's not unicode, then

LPCSTR s = (const char *)qtString;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!