Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, May 2007
How can i convert QString to char *


Message 1 in thread

hi all of you, i have small fundamental doubt... i am unable to convert QString to char *......how can i do that...


D. Anil kumar

Every success has a story of great failure.
So don't stop with failure where
Success comes after failure.

Message 2 in thread

Hi,

On Monday 21 May 2007, anil kumar wrote:
> hi all of you, i have small fundamental doubt... i am unable to convert
> QString to char *......how can i do that...

First convert it to the right encoding:

QByteArray enc=mystring.toUtf8(); //or toLocal8Bit or whatever

//then allocate enough memory:
char *mychar=new char[enc.size()+1];

//and copy it
strcpy(mychar,enc.data());


You can save a bit of memory/hassle if you need the char* only for a short 
time. If you need it only inside the one function you are in, you can use 
the byte array as container and use the data() field directly:

char*mychar=enc.data();

You have to assume the char* to be invalid as soon as enc is no longer 
visible or as soon as you changed the data (the byte array might 
reallocate).

If you need it only for a single function call, you can even use a temporary 
byte array:

printf("string: %s\n",mystring.toAscii().data());

In this case the temporary byte array and with it the char* is invalid at 
the ";" at the end of the line.


	Konrad

Attachment:

Attachment: pgpHKCfmMNulA.pgp
Description: PGP signature


Message 3 in thread

On 5/21/07, Konrad Rosenbaum <konrad@xxxxxxxxx> wrote:
> //and copy it
> strcpy(mychar,enc.data());
(...)
> char*mychar=enc.data();
(...)
> printf("string: %s\n",mystring.toAscii().data());

It is generally much better to use constData() method, than plain
data(), because if the string (or QByteArray) does have a data shared
with some other object, involving non-const method (i.e. data()) will
cause the data to be copied, while const methods (including
constData()) will work on shared data without copying it.
-- 
 [ signature omitted ] 

Message 4 in thread

I'm going to make an unsolicited suggestion here.  From the questions  
you have been asking, it indicates that you are a less experienced  
developer.  Given that, make sure you have a REAL good reason for  
wanting to convert it to a char*.  When you do that, you are probably  
making your program not translatable unless you use a text codec and  
the toUTF8 function as someone else mentioned here.  The biggest  
thing though is that you will probably expose yourself to buffer  
overruns.  I too like the const version of data().

Keep your strings as QStrings.  If you are using a legacy library,  
make sure their isn't a more enlightened library available.

Michael

On May 20, 2007, at 9:29 PM, anil kumar wrote:

>
> hi all of you, i have small fundamental doubt... i am unable to  
> convert QString to char *......how can i do that...
>
>
> D. Anil kumar
>
> Every success has a story of great failure.
> So don’t stop with failure where
> Success comes after failure.
>


Message 5 in thread

> If you need it only for a single function call, you can even use a  
> temporary
> byte array:
>
> printf("string: %s\n",mystring.toAscii().data());
>
> In this case the temporary byte array and with it the char* is  
> invalid at
> the ";" at the end of the line.


So, I think that it would be acceptable to do something like this:

char* myChars = strdup ( myQString.toAscii().data() ) ;

Does anyone see a problem with this ?

--
 [ signature omitted ] 

Message 6 in thread

You can use const char* qPrintable(const QString &) function.

fanda

On Mon, 21 May 2007 06:29:32 +0200, anil kumar <d.anil@xxxxxxxxxxxxx>  
wrote:

>
> hi all of you, i have small fundamental doubt... i am unable to convert  
> QString to char *......how can i do that...
>
>
> D. Anil kumar
>
> Every success has a story of great failure.
> So don't stop with failure where
> Success comes after failure.



-- 
 [ signature omitted ] 

Message 7 in thread

"Fanda Vacek" <fanda.vacek@xxxxxxxx> wrote in message 
news:op.tsoh5imyasg76k@xxxxxxxxxxxxxxxxxxxxxxxx
> You can use const char* qPrintable(const QString &) function.
>
qPrintable() doesn't work with Qt4.2.2 + Visual Studio 2005.  I've
looked at this carefully, stepping through code with the debugger.  It
simply doesn't work.  Try one of the other methods suggested here.



--
 [ signature omitted ] 

Message 8 in thread

On 5/22/07, John Smith <invalid@xxxxxxxxxxx> wrote:
>
> "Fanda Vacek" <fanda.vacek@xxxxxxxx> wrote in message
> news:op.tsoh5imyasg76k@xxxxxxxxxxxxxxxxxxxxxxxx
> > You can use const char* qPrintable(const QString &) function.
> >
> qPrintable() doesn't work with Qt4.2.2 + Visual Studio 2005.  I've
> looked at this carefully, stepping through code with the debugger.  It
> simply doesn't work.  Try one of the other methods suggested here.
Could you elaborate, please, on what exactly do you mean by saying
"doesn't work"?
-- 
 [ signature omitted ]