Qt 4.5 QString have a way to output a null terminaded string ( QString.cstr() )
Qt 4.5 QString have a way to output a null terminaded string ( QString.cstr() )
Could also do this:
Qt Code:
#include <string> To copy to clipboard, switch view to plain text mode
edit*
Qt Code:
To copy to clipboard, switch view to plain text mode
how so? I use this method often to convert QStrings into c-strings and its never seg faulted. In what situtation does it seg fault?
maybe the confusion here is my semi pseudo code:
Qt Code:
const char * c_string = str.toStdString().c_str();To copy to clipboard, switch view to plain text mode
not
Qt Code:
To copy to clipboard, switch view to plain text mode
I agree putting the latter line into code would probably segfault or return NULL to the pointer. Sorry for the confusion.
No, that's still wrong. The whole concept of doing what you are doing is wrong.
Is this the whole "intermediate references smell like poo" thing?
Well could you explain why you think that? I dont see how its any different from converting to a QBtyeArray and pulling the char * out of it.
Well, this is wrong too of course:
Qt Code:
const char *cstr = str.toAscii().constData();To copy to clipboard, switch view to plain text mode
This is much better although might still be wrong in some cases:
Qt Code:
const char *cstr = ba.constData();To copy to clipboard, switch view to plain text mode
This is ok:
So is this:Qt Code:
const char *cstr = strcpy(ba.constData());To copy to clipboard, switch view to plain text mode
Qt Code:
someCallThatNeedsACStr(ba.constData());To copy to clipboard, switch view to plain text mode
Ok i see what you mean now. Yes, if you want a deep copy that you can manipulate safely strcpy() is the way to go. Other than that, this method is effectively the same as using a std::string as the medium. My code was intended for use as an argument to a function that wants a c-str. Also, getting the shallow copy isnt necessarily "wrong" it just depends on what you intend to do with it.
Bookmarks