Results 1 to 3 of 3

Thread: QString to const char

  1. #1
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QString to const char

    i've looked through some of the old posts regarding this subject, and i'm still a bit confused, especially with the bit of code that i'm writing. My question is, will the way i've written this code present problems in the future or will it be ok?

    Qt Code:
    1. //allowed to use Qt-stuff.
    2. const char* getChars(){
    3. QString data = QString("asdf");
    4. return data.toUtf8().constData();
    5. }
    6.  
    7. //not allowed to use Qt-stuff.
    8. //called by methods not seen here that use and rely on the returned variable.
    9. stringStruct checkChars(){
    10. stringStruct str = getChars();
    11. return str;
    12. }
    To copy to clipboard, switch view to plain text mode 

    the stringStruct has the '=' operator overloaded to copy the data with strcpy.

    In the previous threads, it's been said that the toUtf8() function returns a bytearray that dissapears immediately after it is used and that as such, there are no guarantees that the data pointed to by the const char* pointer will be intact. But shouldn't the fact that i immediately (almost) copy the data help me in this situation? Any thoughts? suggestions?

  2. #2
    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 const char

    Quote Originally Posted by TheRonin View Post
    shouldn't the fact that i immediately (almost) copy the data help me in this situation?
    No, it won't help you, because "data" will be destroyed when getChars() returns and you'll get a dangling pointer. No matter how fast you will copy the string, still you'll be accessing deallocated data.

    Either use something like this:
    Qt Code:
    1. const char* getChars(){
    2. QString data = QString("asdf");
    3. return qstrcpy( data.toUtf8().constData() );
    4. }
    5.  
    6. stringStruct checkChars(){
    7. stringStruct str;
    8. str.setBuffer( getChars() );
    9. return str;
    10. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. std::string getChars(){
    2. QString data = QString("asdf");
    3. return std::string( data.toUtf8().constData() );
    4. }
    5.  
    6. stringStruct checkChars(){
    7. stringStruct str = getChars().c_str();
    8. return str;
    9. }
    To copy to clipboard, switch view to plain text mode 
    or simply:
    Qt Code:
    1. stringStruct getChars(){
    2. QString data = QString("asdf");
    3. stringStruct str = data.toUtf8().constData();
    4. return str;
    5. }
    6.  
    7. stringStruct checkChars(){
    8. return getChars();
    9. }
    To copy to clipboard, switch view to plain text mode 

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

    TheRonin (12th February 2007)

  4. #3
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to const char

    very good sir, thank you.

Similar Threads

  1. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  2. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  3. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47
  4. Replies: 4
    Last Post: 24th March 2006, 22:50
  5. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55

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.