const char* 转QString错误

来源:互联网 发布:人工智能企业排名 编辑:程序博客网 时间:2024/05/06 04:51
Qt帮助文件中对QString有一段说明:
As mentioned above, QString provides a lot of functions and operators that make it easy to interoperate with const char * strings. But this functionality is a double-edged sword: It makes QString more convenient to use if all strings are US-ASCII or Latin-1, but there is always the risk that an implicit conversion from or to const char * is done using the wrong 8-bit encoding. To minimize these risks, you can turn off these implicit conversions by defining the following two preprocessor symbols:

QT_NO_CAST_FROM_ASCII disables automatic conversions from C string literals and pointers to Unicode.
QT_NO_CAST_TO_ASCII disables automatic conversion from QString to C strings.
One way to define these preprocessor symbols globally for your application is to add the following entry to your qmake project file:

DEFINES += QT_NO_CAST_FROM_ASCII \
           QT_NO_CAST_TO_ASCII

You then need to explicitly call fromUtf8(), fromLatin1(), or fromLocal8Bit() to construct a QString from an 8-bit string, or use the lightweight QLatin1String class, for example:

QString url = QLatin1String("http://www.unicode.org/");


由此可以得到解决方案:
1.在工程文件(.pro文件)中添加上述蓝色标记的文字,保存
2.重新编译,这时在const char*转QString处会报错('QString::QString(const char*)' is private),参照绿色标记的例子进行修改即可。

更多详情参见Qt中有关QString类的帮助文档。
0 0
原创粉丝点击