Results 1 to 19 of 19

Thread: QString use .c_str()?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    1
    Platforms
    Unix/X11

    Default QString use .c_str()?

    Qt 4.5 QString have a way to output a null terminaded string ( QString.cstr() )

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Could also do this:

    Qt Code:
    1. #include <string>
    2.  
    3. char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    Could also do this:

    Qt Code:
    1. #include <string>
    2.  
    3. char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 
    edit*

    Qt Code:
    1. const char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    This code is incorrect. It will segfault in many situations.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    how so? I use this method often to convert QStrings into c-strings and its never seg faulted. In what situtation does it seg fault?

  7. #7
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    maybe the confusion here is my semi pseudo code:

    Qt Code:
    1. QString str = "Some string";
    2. const char * c_string = str.toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

    not

    Qt Code:
    1. const char * c_string = QString().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

    I agree putting the latter line into code would probably segfault or return NULL to the pointer. Sorry for the confusion.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    No, that's still wrong. The whole concept of doing what you are doing is wrong.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString use .c_str()?

    Is this the whole "intermediate references smell like poo" thing?

  10. #10
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Well could you explain why you think that? I dont see how its any different from converting to a QBtyeArray and pulling the char * out of it.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString use .c_str()?

    Quote Originally Posted by jonthom View Post
    Well could you explain why you think that? I dont see how its any different from converting to a QBtyeArray and pulling the char * out of it.
    Well, this is wrong too of course:
    Qt Code:
    1. QString str = "xxx";
    2. const char *cstr = str.toAscii().constData();
    To copy to clipboard, switch view to plain text mode 

    This is much better although might still be wrong in some cases:
    Qt Code:
    1. QString str = "xxx";
    2. QByteArray ba = str.toAscii();
    3. const char *cstr = ba.constData();
    To copy to clipboard, switch view to plain text mode 

    This is ok:
    Qt Code:
    1. QString str = "xxx";
    2. QByteArray ba = str.toAscii();
    3. const char *cstr = strcpy(ba.constData());
    To copy to clipboard, switch view to plain text mode 
    So is this:
    Qt Code:
    1. QString str = "xxx";
    2. QByteArray ba = str.toAscii();
    3. someCallThatNeedsACStr(ba.constData());
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString use .c_str()?

    Ok i see what you mean now. Yes, if you want a deep copy that you can manipulate safely strcpy() is the way to go. Other than that, this method is effectively the same as using a std::string as the medium. My code was intended for use as an argument to a function that wants a c-str. Also, getting the shallow copy isnt necessarily "wrong" it just depends on what you intend to do with it.

Similar Threads

  1. With QString create a QString&
    By avis_phoenix in forum Newbie
    Replies: 1
    Last Post: 21st April 2010, 22:05
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. QString to hex
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2009, 12:21
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

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.