对于您的问题 #2 - How to convert QString to UTF-16 QByteArray,有一个使用 QTextCodec::fromUnicode() 的解决方案,如以下代码示例所示:
#include <QCoreApplication>
#include <QTextCodec>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// QString to QByteArray
// =====================
QString qstr_test = "test"; // from QString
qDebug().noquote().nospace() << "qstr_test[" << qstr_test << "]"; // Should see: qstr_test[test]
QTextCodec * pTextCodec = QTextCodec::codecForName("UTF-16");
QByteArray qba_test = pTextCodec->fromUnicode(qstr_test); // to UTF-16 QByteArray
qDebug() << "qba_test[";
int test_size = qba_test.size();
for(int i = 0; i < test_size; ++i) { // Should see each UTF-16 encoded character per line like this: ÿþt e s t
qDebug() << qba_test.at(i);
}
qDebug() << "]";
return a.exec();
}
上面的代码已经使用 Qt 5.4 进行了测试。