Hello,
Another really newbie question. What is the appropriate way to get a char * pointer from a QString object? I'm using object.toLocal8Bit().constData(), but doesn't always work (or maybe it doesn't work at all).
- BC
Hello,
Another really newbie question. What is the appropriate way to get a char * pointer from a QString object? I'm using object.toLocal8Bit().constData(), but doesn't always work (or maybe it doesn't work at all).
- BC
It works, just remember it is a const char*. BTW. there is a macro which is a shortcut to the above mentioned combination:
Qt Code:
printf("%s", qPrintable(str));To copy to clipboard, switch view to plain text mode
Please, search the forum before you ask a question. This topic was already discussed here: http://www.qtcentre.org/forum/f-qt-p...ting-3211.html
So, will the qPrintable work, or is the only (best) way to create a QByteArray that doesn't go out of scope? I looked at the previous post and can I assume yours is the only way to do this?
Incidentally, I find the Qt site difficult to search. This forum is excellent, however!
- BC
qPrintable() will only work as a function parameter, so either use:orQt Code:
char *cstr = qstrdup( qPrintable( str ) ); ... delete [] cstr;To copy to clipboard, switch view to plain text modeQt Code:
const char *cstr = ba.constData(); // cstr is valid as long as ba is aliveTo copy to clipboard, switch view to plain text mode
Thanks. I've gone with the second method. Thanks for the help and I apologize that I didn't do a search first.
From the Faq
So is the Faq wrong ??You can use toLatin1() to get a byte array and then call data() on that one which will give you a char*, i.e
toLatin1().data() // Is this wrong ??
http://doc.trolltech.com/4.2/qstring.html#toLatin1
To convert a QString to a char* you can do the following:
QString(QLatin1String(charstringhere))
See the documentation:
http://doc.trolltech.com/4.2/qlatin1string.html
I have tried this program
Qt Code:
#include <iostream> #include <QString> int main(){ std::cout << str.toLatin1().data() << std::endl; const char *charPointer = str.toLatin1().data(); std::cout << charPointer << std::endl; if( charPointer == str.toLatin1().data() ){ std::cout<< "Still pointing to same location "; } return 0; }To copy to clipboard, switch view to plain text mode
My results confirms with the Faq... I am able to get a const pointer to the data() and further calls returns the same memory address ?? Both Wiki and jaceks post discourages us from using this. So who is right here ?
Last edited by sunil.thaha; 1st February 2007 at 09:17.
We can't solve problems by using the same kind of thinking we used when we created them
This creates a temporary object (QByteArray) and returns a pointer to its data, but the temporary object is deleted immediately. The memory is not cleared, the compiler does some optimisations, etc. - the bottom line is the data IS there, but only because you're lucky. If you try to use the pointer after some time the byte array is deleted, you'll eventually receive trash which may cause your app to malfunction. This is strictly related to memory management on your system and to the compiler used. It is not safe to use such constructions in terms of the C++ standard.
You can think of it as the same as if you'd send a signal accross threads in Qt3 - basically it works (unfortunately only "most of the time"), but it doesn't mean that the code is correct.
stevey (14th February 2007)
Please refer to this Faq http://www.trolltech.com/developer/k...30.9032238253/
How can we ask for correction ?
We can't solve problems by using the same kind of thinking we used when we created them
There is nothing wrong in that entry, besides it seems to be an internal Trolltech tool and I'm sure they know about the quirk of storing the volatile pointer.
No, it doesn't say anything about storing the data pointer and only referring to it after the temporary object is destroyed is in violation with the specification. This is a perfectly legitimate statement:
Qt Code:
printf("%s\n", str.toLocal8Bit().data());To copy to clipboard, switch view to plain text mode
whereas this one is not:
Qt Code:
char *ptr = str.toLocal8Bit().data(); printf("%s\n", ptr);To copy to clipboard, switch view to plain text mode
If you want to store the pointer, keep the byte array intact:
Qt Code:
char *ptr = ba.data(); // this is valid as long as "ba" is //... printf("%s\n", ptr);To copy to clipboard, switch view to plain text mode
sunil.thaha (1st February 2007)
Waw !!
Beautiful answer... Thanks a lot for the time and patience.
Long live QtCentre
We can't solve problems by using the same kind of thinking we used when we created them
After this discussions and the subsequent discussion with Qt-Interest mailing list. I had requested the change told above. Trolltech has accepted the change request and has modified the faq
Here is the link to that
http://www.trolltech.com/developer/k...-30.9032238253
Thanks once again.
We can't solve problems by using the same kind of thinking we used when we created them
bruccutler (13th February 2007)
Btw, jacek wrote delete [] cstr, but you dont need to delete a pointer unless you initialized it with new keyword.
From the docs:
char * qstrdup ( const char * src ) Returns a duplicate string.
Allocates space for a copy of src, copies it, and returns a pointer to the copy. If src is 0, it immediately returns 0.
Ownership is passed to the caller, so the returned string must be deleted using delete[]
We can't solve problems by using the same kind of thinking we used when we created them
Bookmarks