PDA

View Full Version : convert char *string to QString



vinithr
18th April 2012, 13:54
hi,

I am new to QT. i am not getting the proper way to convert a char * string to QString that i need to display in lineEdit.

Here is the scenario:

I have a header file my_header.h In that i have a variable named char * string. This variable will update by some other application.

I need to update this variable into a lineEdit that accept only QString.. Could you please tell me how i can convert that to QString.

And I need to do reverse operation also.such as i need to get data from line edit and update in the variable in the header..


Thanks in advance

p3c0
18th April 2012, 14:16
Search for sprintf function in QString.

char *data = "string";
QString str;
str.sprintf("%s",data);
qDebug() << str;

kito
18th April 2012, 15:36
you can just

char* data = "string";
QString str;
str = data;

This should work also.

vinithr
18th April 2012, 15:51
Thanks..it worked for me..

Zlatomir
18th April 2012, 18:10
sprintf is useful for other stuff (http://qt-project.org/doc/qt-4.8/qstring.html#sprintf)
QString has constructor (http://qt-project.org/doc/qt-4.8/qstring.html#sprintf) that takes a char* (and, as kito said, the operator= also can take char*).

ChrisW67
19th April 2012, 01:22
You may need to take into consideration encoding of the C-style string also: is it plain ASCII, Latin 1, UTF-8 or some other supported encoding? QString has a range of static conversion functions for handling these differences if the default constructor/operator=() handling is not quite right.