Char to QString

moesef picture moesef · Jan 11, 2013 · Viewed 34.2k times · Source

I have a char array and want to convert one of the values from char to qstring:

unsigned char inBuffer[64];

....
QString str= QString(*inBuffer[1]);
ui->counter->setText(str);

This isn't working (I get a compiler error). Any suggestions?

Answer

Yuan picture Yuan · Jan 11, 2013

Please check http://qt-project.org/doc/qt-4.8/qstring.html

QString &   operator+= ( char ch )

QString &   operator= ( char ch )

You can use operator+= to append a char, or operator= to assign a char.

But in your code it will call constructor, not operator=. There is no constructor for char, so your code can not compile.

QString str;
str = inBuffer[1];

QString has a constructor

QString ( QChar ch )

So u can use following code to do that

QString str= QChar(inBuffer[1]);

or

QString str(QChar(inBuffer[1]));