[Qt-interest] howto convert QString to char*

Ivan Kharin korsar.akvis at gmail.com
Thu Jul 15 07:06:08 CEST 2010


> you can use this function.

> char *tochar(QString value, int &len)
> {
>     char *mycp = (char *)malloc(value.length()+1);
>     QChar *data = value.data();
>     int ab = 0;
>     while(!data->isNull())
>     {
>         mycp[ab] = data->toLatin1();
>         ++data;
>         ++ab;
>     }
>     mycp[ab] = '\0';
>     len = ab;
>     return mycp;
> }

or just

char *qStringCopy( const QString& s )
{
  return strdup( s.toLocal8Bit().constData() );
}


And using toLatin1 / toLocal8Bit / toUtf8 - depends on situation and platform.


-- 
Regards,
Ivan Kharin





More information about the Qt-interest-old mailing list