[Qt-interest] howto convert QString to char*

Puneet Bisht pbisht at nivio.com
Thu Jul 15 17:59:52 CEST 2010


Hi Matthias,

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;
}

hust call it like this
int len = 0;
char *str = tochar(desiredQString, len);

Thanks
Puneet Bisht


> 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...
>
> Matthias
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>




More information about the Qt-interest-old mailing list