PDA

View Full Version : QString to "char*"



^NyAw^
22nd April 2009, 17:05
Hi,

Why my char pointer don't get the correct value?


QString qName("MyName");
const char* Name = qName.toAscii().data();
//also tryied:
//const char* Name = qName.toAscii().constData();
I think that I did it before using this way.

Thanks,

Lykurg
22nd April 2009, 17:28
I am not 100% sure but qName.toAscii() creating a temporary QByteArray wich is deleted afterwards??? By caching the QByteArray it works...

QString qName("MyName");
QByteArray ba = qName.toAscii();
const char* Name = ba.data();
while (*Name) {
qWarning() << *Name;
++Name;
}


Lykurg

jpn
22nd April 2009, 17:34
What do you need that const char* for? You can just store the QByteArray and pass it to any function that takes a const char*, since QByteArray has const char* operator().

The problem of yours is that you store a pointer to the internal data of a temporary QByteArray object returned by QString::toAscii(). As soon as the QByteArray object goes out of scope, which happens right after the statement, the pointer becomes invalid.

lni
22nd April 2009, 21:39
qPrintable( qName )

Lykurg
22nd April 2009, 21:57
qPrintable( qName )
is expanded like

#ifndef qPrintable
# define qPrintable(string) (string).toLocal8Bit().constData()
#endif
So you have still the problem with the temporal QByteArray!

lni
22nd April 2009, 22:05
is expanded like

#ifndef qPrintable
# define qPrintable(string) (string).toLocal8Bit().constData()
#endif
So you have still the problem with the temporal QByteArray!

I thought he just needs to convert QString to const char* (for output purpose?). If he needs the memory to hold the string value, the "qName" already has it...

Lykurg
22nd April 2009, 22:10
I thought he just needs to convert QString to const char* (for output purpose?). If he needs the memory to hold the string value, the "qName" already has it...
For output ourposes qPrintable is surely the best solution, but sometimes 3rd party libraries needed a char array and therefore you have to go via the "cached" QByteArray, I think...

lni
22nd April 2009, 22:28
For output ourposes qPrintable is surely the best solution, but sometimes 3rd party libraries needed a char array and therefore you have to go via the "cached" QByteArray, I think...

If 3rd party libraries doesn't know QString, it won't know QByteArray either. If you pass QString reference around, I don't see how QByteArray is better. You have qPrintable as well as QString::toStdString for the conversion.

Lykurg
22nd April 2009, 23:50
If 3rd party libraries doesn't know QString, it won't know QByteArray either. If you pass QString reference around, I don't see how QByteArray is better. You have qPrintable as well as QString::toStdString for the conversion.
Look, I don't want to quarrel with you. Your solution with qPrintabel is fine just as the expanded version which the thread starter has used: qName.toAscii().data(). And of course if 3rd party libraries doesn't know QString, it won't know QByteArray either. But if they need a const char* argument qPrintable is no solution because it is not valid for use the array as ^NyAw^ has figured out. Problem is the not "cached" QByteArray mentioned by me and jpn. So you have to create a temporary QByteArray as long as you need a valid const char* array. As long as I understand right now.

QString qName("MyName");
QByteArray ba = qName.toAscii();
const char* Name = ba.data();
const char* NameWithout = qName.toAscii().constData();
// use the array somewhere...
// NameWithout is not valid
// Name is

// destroy ba
// Name is now also invalid

faldzip
23rd April 2009, 07:40
If 3rd party libraries doesn't know QString, it won't know QByteArray either. If you pass QString reference around, I don't see how QByteArray is better. You have qPrintable as well as QString::toStdString for the conversion.
As it was mentioned in one of previous posts, QByteArray has const char * operator, which is casting QByteArray into const char * when there is need for such a cast.

^NyAw^
23rd April 2009, 09:05
Hi,

Sorry for delay.

Thanks for replies.
Catching QByteArray is a good solution. I'm able to understand why my code didn't work.
I'm using a third party lib that needs a "const char*" as argument and I have the value stored into a QString. So, you are right when you suposed it.

Thanks,