
Originally Posted by
lni
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.
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
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
To copy to clipboard, switch view to plain text mode
Bookmarks