QString与printf的%s

qt 4里
QString str("hello");
printf("%s
", str.data()); //只打印出h
//这是由于在qt4里每个char存留unicode,第二字节为零
解决方法
1. printf("%s
", qPrintable(str));
//qPrintable在qt文档里没有出现,但可在其源码里发现
2. printf("%s
", str.toLatin1());
//toLatin1,toUtf8,toLocal8bit返回QByteArray,可以自动转成 char*

Leave a Reply

Your email address will not be published. Required fields are marked *