[Qt-interest] howto convert QString to char*
Gabriel M. Beddingfield
gabrbedd at gmail.com
Tue Jul 13 18:10:02 CEST 2010
On Tue, 13 Jul 2010, Matthias Pospiech wrote:
> I am using the following:
>
> char * toChar(const QString str)
> {
> QByteArray ba = str.toLatin1();
> return ba.data();
> }
>
> the bytearray contains usefull data,
> however the value returned by data() is very useless.
> Is this because ba is destroyed at the end of toChar?
> If so, what would you recommend to make such a conversion?
>
> In my code I use this to write
>
> QString sendSignal = "1.23 1" // this is set somewhere else
> char * str = toChar(sendSignal);
> ...
> // dll functions requiring a char* string...
I'm guessing that when you copy to the QByteArray, that's
where your data is getting copied to a temporary, local
variable.
What about something like this:
inline char* toChar(const QString str) {
return str.toLatin1().data()
}
Or perhaps:
#define toChar(x) ((x).toLatin1().data())
-gabriel
More information about the Qt-interest-old
mailing list