C++ 如何将字符串更改为 QString?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1814189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change string into QString?
提问by NeverAgain
What is the most basic way to do it?
最基本的方法是什么?
采纳答案by Kamil Szot
If by string you mean std::string
you can do it with this method:
如果通过字符串表示std::string
您可以使用此方法执行此操作:
QString QString::fromStdString(const std::string & str)
QString QString::fromStdString(const std::string & str)
std::string str = "Hello world";
QString qstr = QString::fromStdString(str);
If by string you mean Ascii encoded const char *
then you can use this method:
如果通过字符串表示 Ascii 编码,const char *
则可以使用此方法:
QString QString::fromAscii(const char * str, int size = -1)
QString QString::fromAscii(const char * str, int size = -1)
const char* str = "Hello world";
QString qstr = QString::fromAscii(str);
If you have const char *
encoded with system encoding that can be read with QTextCodec::codecForLocale()then you should use this method:
如果您const char *
使用可以使用QTextCodec::codecForLocale()读取的系统编码进行编码,那么您应该使用此方法:
QString QString::fromLocal8Bit(const char * str, int size = -1)
QString QString::fromLocal8Bit(const char * str, int size = -1)
const char* str = "za?ó?? g??l? ja?ń"; // latin2 source file and system encoding
QString qstr = QString::fromLocal8Bit(str);
If you have const char *
that's UTF8 encoded then you'll need to use this method:
如果您使用的const char *
是 UTF8 编码,则需要使用此方法:
QString QString::fromUtf8(const char * str, int size = -1)
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);
There's also method for const ushort *
containing UTF16 encoded string:
还有一种const ushort *
包含 UTF16 编码字符串的方法:
QString QString::fromUtf16(const ushort * unicode, int size = -1)
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);
回答by sth
If compiled with STL compatibility, QString
has a static methodto convert a std::string
to a QString
:
如果与STL兼容编译,QString
有一个静态方法的转换std::string
为QString
:
std::string str = "abc";
QString qstr = QString::fromStdString(str);
回答by shoosh
Alternative way:
替代方式:
std::string s = "This is an STL string";
QString qs = QString::fromAscii(s.data(), s.size());
This has the advantage of not using .c_str()
which might cause the std::string
to copy itself in case there is no place to add the '\0'
at the end.
这样做的优点是不使用.c_str()
它可能会导致std::string
复制自身,以防'\0'
在最后没有地方添加。
回答by wilhelmtell
std::string s = "Sambuca";
QString q = s.c_str();
Warning:This won't work if the std::string
contains \0
s.
警告:如果std::string
contains \0
s,这将不起作用。
回答by Devolus
I came across this question because I had a problem when following the answers, so I post my solution here.
我遇到这个问题是因为我在遵循答案时遇到了问题,所以我在这里发布了我的解决方案。
The above examples all show samples with strings containing only ASCII values, in which case everything works fine. However, when dealing with strings in Windows whcih can also contain other characters, like german umlauts, then these solutions don't work
上面的示例都显示了仅包含 ASCII 值的字符串的示例,在这种情况下,一切正常。但是,在处理 Windows 中的字符串时,其中还可以包含其他字符,例如德语变音符号,则这些解决方案不起作用
The only code that gives correct results in such cases is
在这种情况下给出正确结果的唯一代码是
std::string s = "übernahme";
QString q = QString::fromLocal8Bit(s.c_str());
If you don't have to deal with such strings, then the above answers will work fine.
如果您不必处理此类字符串,那么上述答案将正常工作。
回答by Gabriel de Grimouard
Moreover, to convert whatever you want, you can use the QVariant class.
此外,要转换任何你想要的,你可以使用 QVariant 类。
for example:
例如:
std::string str("hello !");
qDebug() << QVariant(str.c_str()).toString();
int test = 10;
double titi = 5.42;
qDebug() << QVariant(test).toString();
qDebug() << QVariant(titi).toString();
qDebug() << QVariant(titi).toInt();
output
输出
"hello !"
"10"
"5.42"
5
回答by gavinb
Do you mean a C string, as in a char*
string, or a C++ std::string
object?
您是指 C 字符串(如char*
字符串)还是 C++std::string
对象?
Either way, you use the same constructor, as documented in the QT reference:
无论哪种方式,您都使用相同的构造函数,如 QT 参考中所述:
For a regular C string, just use the main constructor:
对于常规 C 字符串,只需使用主构造函数:
char name[] = "Stack Overflow";
QString qname(name);
For a std::string
, you obtain the char*
to the buffer and pass that to the QString
constructor:
对于 a std::string
,您获取char*
缓冲区并将其传递给QString
构造函数:
std::string name2("Stack Overflow");
QString qname2(name2.c_str());