Results 1 to 18 of 18

Thread: QString to char* not converting

  1. #1
    Join Date
    Jul 2006
    Posts
    33
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to char* not converting

    what about QString::ascii() ?
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  3. #3
    Join Date
    Jul 2006
    Location
    Coimbatore,India
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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 *.
    Vichu

  4. #4
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QString to char* not converting

    try QString's function latin1()

  5. #5
    Join Date
    Jul 2006
    Posts
    33
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to char* not converting

    Thanks all,

    The only solution that gave me the proper message was:
    Qt Code:
    1. qStrdup(qPrintable(str));
    To copy to clipboard, switch view to plain text mode 

    All of the others: toLatin, toAscii, and qPrintable, still printed the garbage data.

    Thanks.

  6. #6
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to char* not converting

    or you could do

    Qt Code:
    1. QString foo("bar");
    2. char *baz = foo.toStdString().c_str()
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to char* not converting

    Quote Originally Posted by Methedrine
    or you could do

    Qt Code:
    1. QString foo("bar");
    2. char *baz = foo.toStdString().c_str()
    To copy to clipboard, switch view to plain text mode 
    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.

  8. #8
    Join Date
    Jul 2006
    Posts
    33
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. QString foo("bar");
    2. char *baz = (char*)foo.toStdString().c_str();
    3. cout << "The Message is " << baz << end
    To copy to clipboard, switch view to plain text mode 

    Then I tried this:
    Qt Code:
    1. QString foo("bar");
    2. const char *baz = foo.toStdString().c_str();
    3. cout << "The Message is " << baz << end
    To copy to clipboard, switch view to plain text mode 

    In both cases, it did not work. This is the only conversion that works for me.
    Qt Code:
    1. QString doh("Oops");
    2. char* message = strdup(qPrintable(doh));
    3. cout << "The Message is " << message << endl;
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QString to char* not converting

    How about this?

    Qt Code:
    1. char* message = doh.toLatin1().data();
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. QByteArray ba( doh.toLatin1() );
    2. char *message = ba.data(); // message is valid as long as ba exists.
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jul 2006
    Posts
    33
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?
    Qt Code:
    1. char* message = doh.toLatin1().data();
    To copy to clipboard, switch view to plain text mode 

    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?

    This example also is confusing because I think both cases should show the same data.
    Qt Code:
    1. QString qbar ("Bar");
    2. cout << "Bar string: " << qbar.toAscii ().data () << endl; // Displays Bar
    3. char* copy_qbar = qbar.toAscii ().data ();
    4. cout << "Bar string: " << copy_qbar << endl; // Displays garbage
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    The pointer remains valid as long as the byte array isn't reallocated.
    You should use QString::toLatin1() this way:
    Qt Code:
    1. QString str( "some string" );
    2. QByteArray ba( str.toLatin1() );
    3. char *cstr = ba.data(); // you can use cstr as long as ba exist
    To copy to clipboard, switch view to plain text mode 
    but you do it like this:
    Qt Code:
    1. char *cstr = str.toLatin1().data(); // wrong
    To copy to clipboard, switch view to plain text mode 
    this uses a temporary QByteArray that ceases to exist immediately, so the workaround is to copy that string:
    Qt Code:
    1. char *cstr = qstrdup( str.toLatin1().data() ); // ok, but remember to delete cstr
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to jacek for this useful post:

    DPinLV (3rd August 2006)

  14. #13
    Join Date
    Jul 2006
    Posts
    33
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to char* not converting

    Jacek,

    That clears some things up, thanks, you've been a great help.

    Derrick

  15. #14
    Join Date
    Apr 2006
    Posts
    12
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QString to char* not converting

    const char * data () const (obsolete)

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  17. #16
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  19. #18
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  2. QString ~ QByte error
    By mhoover in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2006, 20:01
  3. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52
  4. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 20:46
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.