如何将字符串转换为QString?

发布于 2024-08-13 01:02:34 字数 17 浏览 4 评论 0原文

最基本的方法是什么?

What is the most basic way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

终难愈 2024-08-20 01:02:34

如果使用 STL 兼容性进行编译,QString 具有静态方法< /a> 将 std::string 转换为 QString

std::string str = "abc";
QString qstr = QString::fromStdString(str);

If compiled with STL compatibility, QString has a static method to convert a std::string to a QString:

std::string str = "abc";
QString qstr = QString::fromStdString(str);
花心好男孩 2024-08-20 01:02:34

如果您所说的字符串是 std::string 您可以使用以下方法来实现:

QString QString::fromStdString(const std::string & str)

std::string str = "Hello world";
QString qstr = QString::fromStdString(str);

如果你所说的字符串是指 Ascii 编码的 const char * 那么你可以使用这个方法:

QString QString::fromAscii(const char * str, int size = -1)

const char* str = "Hello world";
QString qstr = QString::fromAscii(str);

如果您有使用系统编码进行编码的 const char * ,可以使用 QTextCodec::codecForLocale() 那么你应该使用这个方法:

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);

如果您有 UTF8 编码的 const char * 那么您需要使用此方法:

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);

还有包含 UTF16 编码字符串的 const ushort * 方法:

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);

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);

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);

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żółć 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:

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:

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);
做个少女永远怀春 2024-08-20 01:02:34

替代方法:

std::string s = "This is an STL string";
QString qs = QString::fromAscii(s.data(), s.size());

这样做的优点是不使用 .c_str() ,这可能会导致 std::string 复制自身,以防没有地方添加 >'\0' 位于末尾。

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.

表情可笑 2024-08-20 01:02:34
std::string s = "Sambuca";
QString q = s.c_str();

警告:如果 std::string 包含 \0,则此方法将不起作用。

std::string s = "Sambuca";
QString q = s.c_str();

Warning: This won't work if the std::string contains \0s.

微暖i 2024-08-20 01:02:34

我遇到这个问题是因为我在遵循答案时遇到了问题,所以我在这里发布我的解决方案。

上面的示例都显示了仅包含 ASCII 值的字符串的示例,在这种情况下一切正常。但是,当在 Windows 中处理字符串时,其中还可以包含其他字符,例如德语变音符号,那么这些解决方案不起作用

在这种情况下给出正确结果的唯一代码是

std::string s = "Übernahme";
QString q = QString::fromLocal8Bit(s.c_str());

如果您不必处理此类字符串,那么上面的答案会很好地工作。

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

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.

迷爱 2024-08-20 01:02:34

您是指 C 字符串(如 char* 字符串)还是 C++ std::string 对象?

无论哪种方式,您都使用相同的构造函数,如 QT 参考中所述:

对于常规 C 字符串,只需使用主构造函数:

char name[] = "Stack Overflow";
QString qname(name);

对于 std::string,您可以获取缓冲区的 char* 并将其传递给 <代码>QString构造函数:

std::string name2("Stack Overflow");
QString qname2(name2.c_str());

Do you mean a C string, as in a char* string, or a C++ std::string object?

Either way, you use the same constructor, as documented in the QT reference:

For a regular C string, just use the main constructor:

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:

std::string name2("Stack Overflow");
QString qname2(name2.c_str());
差↓一点笑了 2024-08-20 01:02:34

此外,要转换您想要的任何内容,您可以使用 QVariant 类。

例如:

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();

输出

"hello !"
"10"
"5.42"
5

Moreover, to convert whatever you want, you can use the QVariant class.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文