QT之类型转换

Qt在进行数据类型转换时,容易忘记如何使用,或者是早已厌倦了百度QString转QByteArray,QByteArray转char*,QString转string......。
现在分享一篇QT数据类型之间的转换。
1、QString转QByteArray
QByteArray byte;
QString string;
byte = string.toLatin1();
2、QByteArray转QString
QByteArray byte;
QString string;
string = QString(byte);
3、QString转String
QString string;
std::string str;
str = string.toStdString();
4、String转QString
QString string;
std::string str;
string = QString::fromStdString(str);
5、QByteArray转char *
char *ch; //不能定义成ch[n],数组和指针不一样;
QByteArray byte;
ch = byte.data();
6、char *转QByteArray
char *ch;QByteArray byte;
byte = QByteArray(ch);
7、QString转char *
QString string;char *ch;
ch = string.toLatin1.data();
8、char *转QString
char *ch;
QString string= QString(QLatin1String(ch));
9、QHostAddress转QString
QHostAddress address;
QString string = address.toString();
10、QString转QHostAddress
QString string;
QHostAddress address(string);
11、QString转unsigned char
QString string;
bool ok;
unsigned char uch = string.toInt(&ok, 16);
12、unsigned char转QString
unsigned char uch;
QString string = QString::number(uch,16);
13、int转QString
int m;
QString string;string=QString::number(m);
14、QString转int
QString string;
int m;
m=string.toInt();

原文地址:https://www.cnblogs.com/heych/p/11285361.html