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

Qt-interest Archive, February 2007
Help with QString and C interfaces


Message 1 in thread

Ok, so here is my problem.  I have a project, that has finally been given the go ahead to port to QT 4.2 from QT 3.3.
 
In the transform, I have also made the decision to move from STL strings to QStrings.  The original architecture, was QT only in the GUI, but QT is now all throughout the core, and it just didn't make sense anymore to keep that restriction in place.
 
And since I had a 4 month window for the port, I figured adding this conversion would not be a problem...
 
Well, here is my issue, I have a bunch of OEM C interface libraries that take in a char *.
 
But since, String only returns a temporary QByteArray (for toAscii) or a const QChar * for constData (which would work if it was a const QChar * :( ) Im getting frustrated.
 
The question comes down to, what is the QT prefered way to interface a QString into a C const char * world?  If it comes down to copy the string into a char buffer, Ill pass on using it here, and go back to my stl string for these strings.

 

TIA


Scott

--
 [ signature omitted ] 

Message 2 in thread

On Saturday 24 February 2007 05:07, Scott Aron Bloom wrote:
> Ok, so here is my problem.  I have a project, that has finally been given
> the go ahead to port to QT 4.2 from QT 3.3.
>
> In the transform, I have also made the decision to move from STL strings to
> QStrings.  The original architecture, was QT only in the GUI, but QT is now
> all throughout the core, and it just didn't make sense anymore to keep that
> restriction in place.
>
> And since I had a 4 month window for the port, I figured adding this
> conversion would not be a problem...
>
> Well, here is my issue, I have a bunch of OEM C interface libraries that
> take in a char *.
What are you passing in the char*? Is it really a string, or it is a byte 
array? If it is a string, is it UTF8, just 8 bit clean, or pure ascii? Are 
you just trying to show something vaguelly sensible to the user?

If you are just looking to do a best effort conversion from a unicode string 
(aka QString) to an 8 bit-clean representation, something like
QString foo ("bar");
function_taking_char_array( foo.toLocal8Bit1().data() );
might be enough.

Brad

Attachment:

Attachment: pgplJmgeTk60a.pgp
Description: PGP signature


Message 3 in thread

On Fri, 23 Feb 2007 10:07:37 -0800
"Scott Aron Bloom" <scott@xxxxxxxxxxxx> wrote:

> But since, String only returns a temporary QByteArray (for toAscii)
> or a const QChar * for constData (which would work if it was a const
> QChar * :( ) Im getting frustrated. 

I usually refer to mystring.toAscii().constData() 
Since it uses a temporary object, you should be aware that the pointer
must be used within the statment itself, before the temporary
QByteArray goes out of scope.

HTH

ing. Federico Fuga

--
 [ signature omitted ] 

Message 4 in thread

> -----Original Message-----
> From: ing. Federico Fuga [mailto:fuga@xxxxxxxxxxxxxx]
> Sent: Saturday, February 24, 2007 2:19 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: Help with QString and C interfaces
> 
> On Fri, 23 Feb 2007 10:07:37 -0800
> "Scott Aron Bloom" <scott@xxxxxxxxxxxx> wrote:
> 
> > But since, String only returns a temporary QByteArray (for toAscii)
> > or a const QChar * for constData (which would work if it was a const
> > QChar * :( ) Im getting frustrated.
> 
> I usually refer to mystring.toAscii().constData()
> Since it uses a temporary object, you should be aware that the pointer
> must be used within the statment itself, before the temporary
> QByteArray goes out of scope.
> 
> HTH
> 
> ing. Federico Fuga
> 
Yes... I understood this, thus the comment :(



--
 [ signature omitted ] 

Message 5 in thread

Am Freitag, 23. Februar 2007 19:07 schrieb Scott Aron Bloom:
> Ok, so here is my problem.  I have a project, that has finally been given
> the go ahead to port to QT 4.2 from QT 3.3.
>
>
> The question comes down to, what is the QT prefered way to interface a
> QString into a C const char * world?  If it comes down to copy the string
> into a char buffer, Ill pass on using it here, and go back to my stl string
> for these strings.
>

The problem with QString is, that it holds a more cooked representation of 
characters, together with some encoding, while the classic char* and the stl 
strings hold the real 8bit sequence formerly called byte.

So whenever you fiddle with bytes, you should keep them in stl strings or just 
leave them in the char*.  Whenever you operate on text, QString should be 
much easier in some parts, especially when you need to convert Text between 
some encodings.

--
 [ signature omitted ] 

Message 6 in thread

> > Ok, so here is my problem.  I have a project, that has finally been
> given
> > the go ahead to port to QT 4.2 from QT 3.3.
> >
> >
> > The question comes down to, what is the QT prefered way to interface
a
> > QString into a C const char * world?  If it comes down to copy the
> string
> > into a char buffer, Ill pass on using it here, and go back to my stl
> string
> > for these strings.
> >
> 
> The problem with QString is, that it holds a more cooked
representation of
> characters, together with some encoding, while the classic char* and
the
> stl
> strings hold the real 8bit sequence formerly called byte.
> 
> So whenever you fiddle with bytes, you should keep them in stl strings
or
> just
> leave them in the char*.  Whenever you operate on text, QString should
be
> much easier in some parts, especially when you need to convert Text
> between
> some encodings.

And from Brad
> >
> > Well, here is my issue, I have a bunch of OEM C interface libraries
that
> > take in a char *.
> What are you passing in the char*? Is it really a string, or it is a
byte
> array? If it is a string, is it UTF8, just 8 bit clean, or pure ascii?
Are
> you just trying to show something vaguelly sensible to the user?
> 
> If you are just looking to do a best effort conversion from a unicode
> string
> (aka QString) to an 8 bit-clean representation, something like
> QString foo ("bar");
> function_taking_char_array( foo.toLocal8Bit1().data() );
> might be enough.
> 
> Brad

The problem is, the string being generated is a manipulated string, and
I want to use the QString convenience functionality..

For instance, in one case, my object computes a string to return to a
licensing engine.

However the licensing engine has a C interface...

The solution I found, was rather then using QString, use QByteArray
directly.  It has most of the qString interface, but no temporary
objects 
when going to C

Scott

--
 [ signature omitted ] 

Message 7 in thread

It all depends on how your C interface documents string ownership.

If the licensing engine has a C function

bool validateLicense(char *id)

then call this with

if (validateLicense(qstring.toLatin1().data())) ...

and pray that validateLicense doesn't overwrite the memory you pass in. If 
the engine has a C function

bool validateLicenseLater(char *id, MyCallback foo);

(i.e. you pass in some ID, the engine makes a lookup on some license 
server, and calls your callback), then you need to keep the data alive 
unless the C interface says that it will make a deep copy (which would be 
reasonable).

In this case, either keep the QByteArray alive for the duration of the 
function call, or make a deep copy of the byte copy using malloc/free. 
Using std::string doesn't help here, because you will have to keep that 
one alive instead of the QByteArray.

Using a QByteArray all the time might be the best solution if you don't 
care about i18n support in those APIs. QByteArray's API is very close to 
QString's as far as text manipulation is concerned.

Volker


"Scott Aron Bloom" <scott@xxxxxxxxxxxx> wrote in message 
news:E2E5EA152B64E044B464DA705AF44AAE7015@xxxxxxxxxxxxxxxxxxxxxx
Well, here is my issue, I have a bunch of OEM C interface libraries that 
take in a char *.

But since, String only returns a temporary QByteArray (for toAscii) or a 
const QChar * for constData (which would work if it was a const QChar * 
:( ) Im getting frustrated.

The question comes down to, what is the QT prefered way to interface a 
QString into a C const char * world?  If it comes down to copy the string 
into a char buffer, Ill pass on using it here, and go back to my stl 
string for these strings.



TIA


Scott

--
 [ signature omitted ] 

Message 8 in thread

> 
> Using a QByteArray all the time might be the best solution if you
don't
> care about i18n support in those APIs. QByteArray's API is very close
to
> QString's as far as text manipulation is concerned.
> 
> Volker
> 
That's exactly what I wound up doing... Part of the problem, for many of
the calls, it went something like

const char * cStr = stlstring.c_str();
CCall1( cStr,....);
CCall2( cStr,....) 
Etc
Etc
Etc

Meaning, there was no way, without making a const char * copy, to use
QString, but replacing the stlstrng with a ByteArray worked just fine

Thanks

Scott

--
 [ signature omitted ]