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:
Qt Code:
  1. QString doh = ("Blah, blah, blah");
  2. cout << "The String " << qPrintable(doh) << endl;
  3.  
  4. // Convert to char*
  5. const char *message = doh.toLocal8Bit ().constData (); //<--- DOES NOT WORK!!
  6. cout << "The Message is " << message << endl
To copy to clipboard, switch view to plain text mode 
;

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:
Qt Code:
  1. QString doh = ("Blah, blah, blah");
  2. cout << "The String " << qPrintable(doh) << endl;
  3.  
  4. // Convert to char*
  5. const char *message = qPrintable(doh); //<--- DOES NOT WORK!!
  6. cout << "The Message is " << message << endl
To copy to clipboard, switch view to plain text mode 
;

The only way I get legit output is doing the following:
Qt Code:
  1. QString doh = ("Blah, blah, blah");
  2. cout << "The String " << qPrintable(doh) << endl;
  3.  
  4. // Convert to char*
  5. QTextCodec *codec = QTextCodec::codecForName("UTF-8");
  6. QByteArray encodedString = codec->fromUnicode(doh);
  7. const char *message = encodedString.constData ();
  8. cout << "The Message is " << message << endl;
To copy to clipboard, switch view to plain text mode 

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?

Thanks,
Derrick