qt之数据类型转换

目录

1.QString 与 char* 互转

2.QByteArray 与 QString 互转

3.QString 与 QJsonDocument 互转

4.QString 与 const char *互转

5.QString 与 wchar_t* 互转


1.QString 与 char* 互转

QString qstr = "Hello World!";
char *a = qstr.toLatin1().data();
char *a = "Hello World!";
QString qstr = QString(QLatin1String(a));

2.QByteArray 与 QString 互转

QString qstr = "Hello World!";
QByteArray qba = qstr.toLatin1();
QByteArray qba = "Hello World!";
QString qstr = QString::fromLatin1(qba);

3.QString 与 QJsonDocument 互转

QJsonParseError error;
QString qstr = "[]";
QJsonDocument qdoc = QJsonDocument::fromJson(qstr.toUtf8(),&error);
 QJsonParseError error;
 QJsonDocument qdoc;
 QString qstr = QString(QJsonDocument(qdoc).toJson());

4.QString 与 const char *互转

QString qstr = "Hello World!";
const char * a = qstr.toStdString().c_str();
QString string = QString(QLatin1String(a))

5.QString 与 wchar_t* 互转

QString str = "Hello World!";
wchar_t Wt[1024];
	wcscpy_s(reinterpret_cast(Wt),sizeof(Wt) / sizeof(wchar_t),
		reinterpret_cast(str.utf16()));
QString getString = QString::fromWCharArray(Wt);

 

 

 

 

 

你可能感兴趣的:(C++)