How to convert QList<QByteArray> to QString in QT?
I have a 开发者_开发百科QList<QByteArray>
that I want to print out in a QTextBrowser
. QTextBrowser->append() takes a QString.
Despite a ton of searching online, I have not found a way to convert the data I have into a QString.
There are several functions to convert QByteArray to QString: QString::fromAscii(), QString::fromLatin1(), QString::fromUtf8() etc. for the most common ones, and QTextCodec for other encodings. Which one is the correct one depends on the encoding of the text data in the byte array.
Try:
for(int i=0; i<list.size(); ++i){
QString str(list[i].constData());
// use your string as needed
}
from QByteArray
to QString
, do
const char * QByteArray::constData () const
Returns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the array. The data is '\0'-terminated. The pointer remains valid as long as the byte array isn't reallocated or destroyed.
This function is mostly useful to pass a byte array to a function that accepts a const char *.
you then have this QString
constructor
QString ( const QChar * unicode )
精彩评论