问题描述
最基本的方法是什么?
推荐答案
如果你说的字符串是 std::string
你可以用这个方法:
If by string you mean std::string
you can do it with this method:
QString QString::fromStdString(const std::string & str)
std::string str = "Hello world";
QString qstr = QString::fromStdString(str);
<小时>
如果你的字符串是 Ascii 编码的 const char *
那么你可以使用这个方法:
If by string you mean Ascii encoded const char *
then you can use this method:
QString QString::fromAscii(const char * str, int size = -1)
const char* str = "Hello world";
QString qstr = QString::fromAscii(str);
<小时>
如果你有 const char *
用系统编码编码,可以用 QTextCodec::codecForLocale() 那么你应该使用这个方法:
If you have const char *
encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use this method:
QString QString::fromLocal8Bit(const char * str, int size = -1)
const char* str = "zaó gl jań"; // latin2 source file and system encoding
QString qstr = QString::fromLocal8Bit(str);
<小时>
如果你有 const char *
是 UTF8 编码的,那么你需要使用这个方法:
If you have const char *
that's UTF8 encoded then you'll need to use this method:
QString QString::fromUtf8(const char * str, int size = -1)
const char* str = read_raw("hello.txt"); // assuming hello.txt is UTF8 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const char*
QString qstr = QString::fromUtf8(str);
<小时>
还有一个方法用于 const ushort *
包含 UTF16 编码的字符串:
There's also method for const ushort *
containing UTF16 encoded string:
QString QString::fromUtf16(const ushort * unicode, int size = -1)
const ushort* str = read_raw("hello.txt"); // assuming hello.txt is UTF16 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const ushort*
QString qstr = QString::fromUtf16(str);
这篇关于如何将字符串更改为 QString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!