QT编程中windows下的char*,wchar_t*与QString之间的相互转换

QString fileName("我是中文abc!");

//QString to wchar_t: 

const wchar_t * encodedName = reinterpret_cast(fileName.utf16()); 

 

//QString to char * (general case): 

const char * tmp = str.toUtf8().constData(); 

 

//wchar_t* to QString

wchar_t szMsg[100] = {0};

_stprintf(szMsg,L"Connect [%s] failed!",ssid);

QString str1= QString::fromWCharArray(szMsg);

 

//char* to QString

QStirng str2(tr("char* 转 QString."));

 

 

以下是有用的简单宏实现

#ifdef UNICODE
    #define QStringToTCHAR(x)     (wchar_t*) x.utf16()
    #define PQStringToTCHAR(x)    (wchar_t*) x->utf16()
    #define TCHARToQString(x)     QString::fromUtf16((x))
    #define TCHARToQStringN(x,y)  QString::fromUtf16((x),(y))
#else
    #define QStringToTCHAR(x)     x.local8Bit().constData()
    #define PQStringToTCHAR(x)    x->local8Bit().constData()
    #define TCHARToQString(x)     QString::fromLocal8Bit((x))
    #define TCHARToQStringN(x,y)  QString::fromLocal8Bit((x),(y))
#endif

你可能感兴趣的:(经验)