QString unsigned char* 的转换

QString -> unsigned char* :
QString str = "ABCD";
 int length = str.length();
unsigned char* sequence = NULL;
sequence =(unsigned char*)qstrdup(str.toAscii().constData());
delete[] sequence;
- sequence length = 5 --> ['A'] ['B'] ['C'] ['D'] ['/0']
- sequence is now "independant" from str
- sequence has to be deleted with -> delete [] sequence

QString -> char:
const QByteArray ba = string.toAscii(); // make ba const, because modifying this array might otherwise invalidate the pointer
const char* sequence = ba.constData(); // now sequence will remain valid within the current scope.

The call to toAscii() creates a temporary QByteArray which goes out of scope when used like this:
char *sequence = string.toAscii().constData();
// sequence is now a dangling pointer!


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yhtoo/article/details/6157032
文章标签: delete null c
个人分类: QT
上一篇Linux解压rar文件
下一篇UNIX培训资料
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭