PDA

View Full Version : QString to char*



giginjose
1st June 2010, 16:40
Hi,

How can I convert QString to char*. I need to use char*, since my development SDK supports only char* for the particular API.

Please do help.

Thanks

GIGIN JOSE

Zlatomir
1st June 2010, 17:16
Like this:


QString test = "test test test";
const char *converted = test.toLatin1().data();

xavierda
2nd June 2010, 03:55
You may also want to check out the declaration qPrintable (http://doc.trolltech.com/latest/qtglobal.html#qPrintable).

Xav.

bhaskar
2nd June 2010, 06:27
Hi Gigin Jose,

If U work with different languages like(korea,china,thai )
Better to use like below in commersial embedded applications in Linux plat form.

QString strName("Test string Data");
char * chrStr = strName.toLocal8Bit().data();

ChrisW67
2nd June 2010, 07:37
Storing the result of
char *dangling_pointer = strName.toLocal8Bit().data() and variants is dangerous because data() returns a pointer to a temporary QByteArray's data. That QByteArray goes out of scope at the end of the assignment statement leaving a dangling pointer. If you cannot use the intermediate value immediately then best to make the QByteArray explicit. Either:


res = somefunc(strName.toLocal8Bit().data());

or


QByteArray ba = strName.toLocal8Bit();
// intervening stuff
res = somefunc(ba.data());
should be safe (but I am fairly new to the C++ game and happy to be corrected).

SneakyPeterson
11th June 2010, 10:19
qPrintable(your QString)