std::string 转 Qstring
时间: 2023-07-08 20:04:32 浏览: 92
可以使用QString::fromStdString()函数将std::string转换为QString。
例如:
```c++
#include <iostream>
#include <string>
#include <QString>
int main()
{
std::string str = "Hello, world!";
QString qstr = QString::fromStdString(str);
std::cout << "std::string: " << str << std::endl;
std::cout << "QString: " << qstr.toStdString() << std::endl;
return 0;
}
```
输出结果:
```
std::string: Hello, world!
QString: Hello, world!
```
相关问题
std::string转qstring
std::string转qstring的方法有多种,可以使用QString的fromStdString函数或者fromLocal8Bit函数。对于包含中文的情况,可以使用fromLocal8Bit函数来避免乱码问题。下面是示例代码:
```cpp
// 使用fromStdString函数
std::string str_original("Hello World");
QString qs = QString::fromStdString(str_original);
// 使用fromLocal8Bit函数
std::string str_original("中文测试");
QString qs = QString::fromLocal8Bit(str_original.c_str());
```
需要注意的是,如果字符串中包含特殊字符或者非ASCII字符,使用fromStdString函数可能会导致乱码问题。因此,在处理包含中文的情况下,建议使用fromLocal8Bit函数来保证字符的正确显示。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [VS2010+QT5编程之QString与char*的相互转换](https://blog.csdn.net/godmaycry/article/details/46372153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [QString与std::string 有中文时的转换操作](https://blog.csdn.net/qq_22642239/article/details/124297095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [QT基础 - QString 与 std::string 之间类型转换](https://blog.csdn.net/limeigui/article/details/123693102)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
std::string 转QSTring
可以使用QString::fromStdString()函数将std::string类型转换为QString类型,示例代码如下:
```
std::string str = "Hello, world!";
QString qstr = QString::fromStdString(str);
```
阅读全文