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

 

posted @ 2019-01-01 02:37  jadeshu  阅读(4740)  评论(0编辑  收藏  举报