Results 1 to 18 of 18

Thread: QString to char* not converting

Hybrid View

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

    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 

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

    DPinLV (3rd August 2006)

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

    Default Re: QString to char* not converting

    Jacek,

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

    Derrick

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

    Default Re: QString to char* not converting

    const char * data () const (obsolete)

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

    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.

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 09:55
  2. QString ~ QByte error
    By mhoover in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2006, 21:01
  3. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 15: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, 21:46
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16: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
  •  
Qt is a trademark of The Qt Company.