1 整形 转 QString
int number = 101;
QString strnumber = QString("%1").arg(number);
qDebug()<<"strnumber:"<<strnumber;
1.2 QString 转 整形
int numberstr = strnumber.toInt();
qDebug()<<"numberstr:"<<numberstr;
2.1 QByteArray 转 QString
QByteArray ba = "asdasd";
QString strba = QString(ba);
qDebug()<<"strba:"<<strba;
2.2 QString 转 QByteArray
QByteArray bastr = strba.toLatin1();
qDebug()<<"bastr:"<<bastr;
2.3 QString 转 string
QString qstr;
std::string = QString::toStdString(string);
2.4 string 转QString
std::string = str_info;
QString qstr = QString::fromStdString(string);
3.1 char 转 QString
char* ch = "hello";
QString strch = QString("%1").arg(ch);
qDebug()<<"strch:"<<strch;
3.2 QString 转 char
std::string str = strch.toStdString();
const char* chstr = str.c_str();
qDebug()<<"chstr:"<<chstr;
4.1、结构体转QByteArray
struct Head head;
head.fileName = m_fileName;
head.fileSize = m_fileSize;
QByteArray data;
data.resize(sizeof(Head));
memcpy(data.data(), &head, sizeof(Head));
4.2、QByteArray转结构体
QByteArray array = "xxxx";
Head head;
char *src = array.data();
memcpy(&head, src, sizeof(Head));
5 字符串分割
QString tmpstr = "吃葡萄不吐葡萄皮,不吃葡萄到吐葡萄皮";
QStringList list = tmpstr.split("葡萄");
for (int var = 0; var < list.length(); ++var) {
qDebug()<<"list["<<var<<"]="<<list[var];
}
6 字符串匹配子串替换
QString tmpstr_2 = "吃葡萄不吐葡萄皮,不吃葡萄到吐葡萄皮";
tmpstr_2 = tmpstr_2.replace("葡萄", "荔枝");
qDebug()<<"tmpstr_2:"<<tmpstr_2;