QString to char* not converting
Hi,
I see some suggestions for how to convert a QString to a char* (or const char*), but when I try them I get bad data.
For instance, I try this:
Code:
QString doh
= ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
const char *message = doh.toLocal8Bit ().constData (); //<--- DOES NOT WORK!!
cout << "The Message is " << message << endl
;
and the output looks like as follows:
The String Blah, blah, blah
The Message is ▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌
Then, if I try this the output is the same:
Code:
QString doh
= ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
const char *message = qPrintable(doh); //<--- DOES NOT WORK!!
cout << "The Message is " << message << endl
;
The only way I get legit output is doing the following:
Code:
QString doh
= ("Blah, blah, blah");
cout << "The String " << qPrintable(doh) << endl;
// Convert to char*
QByteArray encodedString
= codec
->fromUnicode
(doh
);
const char *message = encodedString.constData ();
cout << "The Message is " << message << endl;
The output is then;
The String Blah, blah, blah
The Message is Blah, blah, blah
This can't be the cleanest way to accomplish getting the proper character string into my char*, can it? :confused:
Thanks,
Derrick
Re: QString to char* not converting
what about QString::ascii() ?
Re: QString to char* not converting
hi,
just try this code,
QString str=" ";
char *st=qStrdup(qPrintable(str));
it convert QString to cons char or char *.
Re: QString to char* not converting
try QString's function latin1()
Re: QString to char* not converting
Thanks all,
The only solution that gave me the proper message was:
Code:
qStrdup(qPrintable(str));
All of the others: toLatin, toAscii, and qPrintable, still printed the garbage data.
Thanks.
Re: QString to char* not converting
or you could do
Code:
char *baz = foo.toStdString().c_str()
Re: QString to char* not converting
Quote:
Originally Posted by Methedrine
or you could do
Code:
char *baz = foo.toStdString().c_str()
And it would fail for the same reason as other methods --- you create a temporary std::string, obtain a pointer to its internal data and then... std::string gets destroyed leaving a dangling pointer.
Re: QString to char* not converting
Methedrine,
Thanks for the suggestion but for some reason my char* string appears to contain 16bit code. I also had to change a few things to make it compile.
First of all I tried this:
Code:
char *baz = (char*)foo.toStdString().c_str();
cout << "The Message is " << baz << end
Then I tried this:
Code:
const char *baz = foo.toStdString().c_str();
cout << "The Message is " << baz << end
In both cases, it did not work. This is the only conversion that works for me.
Code:
char* message = strdup(qPrintable(doh));
cout << "The Message is " << message << endl;
Re: QString to char* not converting
How about this?
Code:
char* message = doh.toLatin1().data();
Re: QString to char* not converting
Quote:
Originally Posted by Byngl
How about this?
char* message = doh.toLatin1().data();
The same problem. QString::toLatin1() returns QByteArray, which will be destroyed just after that line, so either you have to immediately create a copy of that char * or use:
Code:
char *message = ba.data(); // message is valid as long as ba exists.
Re: QString to char* not converting
Jacek,
Can you point me to some documentation that explains QStrings and QByteArrays better? Because I don't see why the byte array should be deleted here since it (or it's pointer) is still in scope, no?
Code:
char* message = doh.toLatin1().data();
The QByteArray help says, "QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects", so shouldn't it be available to me to display? :confused:
This example also is confusing because I think both cases should show the same data.
Code:
cout << "Bar string: " << qbar.toAscii ().data () << endl; // Displays Bar
char* copy_qbar = qbar.toAscii ().data ();
cout << "Bar string: " << copy_qbar << endl; // Displays garbage
Thanks.
Re: QString to char* not converting
Quote:
Originally Posted by DPinLV
I don't see why the byte array should be deleted here since it (or it's pointer) is still in scope, no?
The pointer is in scope, but the temporary QByteArray isn't. QString::toLatin1() returns a completely new QByteArray object --- not some reference to an internal buffer. On the other hand QByteArray::data() returns a pointer to internal data of that QByteArray, so if it gets destroyed, that pointer will be invalid.
Quote:
Originally Posted by DPinLV
The QByteArray help says, "QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects", so shouldn't it be available to me to display?
It also says:
Quote:
The pointer remains valid as long as the byte array isn't reallocated.
You should use QString::toLatin1() this way:
Code:
char *cstr = ba.data(); // you can use cstr as long as ba exist
but you do it like this:
Code:
char *cstr = str.toLatin1().data(); // wrong
this uses a temporary QByteArray that ceases to exist immediately, so the workaround is to copy that string:
Code:
char *cstr = qstrdup( str.toLatin1().data() ); // ok, but remember to delete cstr
Re: QString to char* not converting
Jacek,
That clears some things up, thanks, you've been a great help.
Derrick
Re: QString to char* not converting
const char * data () const (obsolete)
Re: QString to char* not converting
Quote:
Originally Posted by lum
const char * data () const (obsolete)
It was available in Qt3, but this thread is about Qt4.
Re: QString to char* not converting
Quote:
Originally Posted by DPinLV
Methedrine,
Thanks for the suggestion but for some reason my char* string appears to contain 16bit code.
Then you are looking for .toStdWString().c_str() or however it's cased.
Re: QString to char* not converting
Quote:
Originally Posted by Methedrine
Then you are looking for .toStdWString().c_str() or however it's cased.
This also suffers from the same problem as other .toX().pointerToInternalData() constructs.
Re: QString to char* not converting
Quote:
Originally Posted by jacek
This also suffers from the same problem as other .toX().pointerToInternalData() constructs.
Sorry, my mistake. I got his problem wrong :o