本文记录QT常用的代码片段。如打印、字符转换,等等。
打印
1 2 3
| #include <QDebug> qDebug("%x %d", a, b); qDebug() << "hello";
|
QString和QByteArray
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| //Qt5.3.2 QString str("hello"); QByteArray bytes = str.toUtf8(); // QString转QByteArray方法1 QString str("hello"); QByteArray bytes = str.toLatin1(); // QString转QByteArray方法2
//Qt5.3.2 QByteArray bytes("hello world"); QString string = bytes; // QByteArray转QString方法1
QByteArray bytes("hello world"); QString string; string.prepend(bytes);// QByteArray转QString方法2
小结:数组转字符串,直接赋值。字符串转数组,用toLatin1或toUtf8
数组转十六进制字符串 QByteArray sendData QString showStr showStr = sendData.toHex(' ').data(); // 用空格隔开
|
获取本机IPv4地址
1 2 3 4 5 6 7 8 9 10
| QStringList list; QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); foreach (QHostAddress addr, ipAddressesList) { if (addr.toIPv4Address()) { qDebug() << addr.toString(); list.append(addr.toString()); } }
|