QString和char*互转

1. QString转为char * 

// QString转QByteArray
QByteArray sr = strQ.toLocal8Bit();
int len = sr.length();
char* buf = new char[len+2];
buf[len] = buf[len+1] = 0;

// QByteArray转char*
strcpy(buf,sr.data());

2. char *转为QString

char *ch="hello!";
QString str(ch);   // Qt5     
QString str = QString::fromUtf8(ch));    //  Qt4

 

你可能感兴趣的:(QT)