std::string转qstring
时间: 2023-09-14 11:11:00 浏览: 109
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 ]
阅读全文