如何将字符串更改为QString? [英] How to change string into QString?

查看:228
本文介绍了如何将字符串更改为QString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最基本的方法是什么?

推荐答案

如果按字符串表示 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(con​​st 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żółć gęślą 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);






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

这篇关于如何将字符串更改为QString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆