Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char to QString

Tags:

c++

qt

qstring

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?

like image 700
moesef Avatar asked Jan 11 '13 02:01

moesef


2 Answers

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]));
like image 65
Yuan Avatar answered Sep 30 '22 21:09

Yuan


This is the easiest way to do that:

QString x="";
QChar y='a';

x+=y;

So here you have a QString with the char.

like image 29
Jherson Sazo Avatar answered Sep 30 '22 20:09

Jherson Sazo