Results 1 to 17 of 17

Thread: how to conver QString to const char*

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to conver QString to const char*

    y QByteArray created by the toAscii() method is destroyed.
    True, however it is implicitly shared, so the pointer it was pointing at still lives, as long as the original 's' is alive.(unless you have done something to alter the content of the pointer, which is why const is a better choice).

    At any rate, my suggestion was wrongly understood.
    If you read my original post, you will see the syntax is not correct too.
    I just meant that one could use toAscii() by getting the QByteArray from the QString, that is all.
    So when you do it, make sure you are not working with the temp instance of it.
    But the reaction "YOU SHOULD NEVER..." assertion starts to annoy me, (it gets more and more to be seen around here).
    If you should never call a specific function, it should not exist.
    If it does, you should call it, in constraints of its usage and when its appropriate - that was my point.
    If it is, or not appropriate in a specific case, is another issue.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to conver QString to const char*

    Quote Originally Posted by high_flyer View Post
    True, however it is implicitly shared, so the pointer it was pointing at still lives, as long as the original 's' is alive.(unless you have done something to alter the content of the pointer, which is why const is a better choice).
    You are retrieving a pointer to a data buffer inside the temporary QByteArray not a pointer to the buffer underlying the QString. The data buffer in the temporary QByteArray is a transformed copy of the data buffer that is in the string's buffer. That buffer is not shared with the string and goes away with the temporary QByteArray.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to conver QString to const char*

    Just to be clear: my point was NOT about using a temp objects.

    Back to our discussion:
    The data buffer in the temporary QByteArray is a transformed copy of the data buffer that is in the string's buffer.
    I just checked and you are right:
    toAscii() (via toLatin1()) indeed transforms the data:
    Qt Code:
    1. QByteArray QString::toLatin1() const
    2. {
    3. if (d->size) {
    4. ba.resize(d->size);
    5. const ushort *i = d->data;
    6. const ushort *e = d->data + d->size;
    7. uchar *s = (uchar*) ba.data();
    8. while (i != e) {
    9. *s++ = (*i>0xff) ? '?' : (uchar) *i;
    10. ++i;
    11. }
    12. }
    13. return ba;
    14. }
    To copy to clipboard, switch view to plain text mode 

    One of the main disadvantages answering such post during work is that I can often just skim the posts. :-\
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. How to convert QString to const char*
    By fulin in forum Newbie
    Replies: 7
    Last Post: 9th May 2010, 23:25
  2. char to const char* with atof
    By mickey in forum General Programming
    Replies: 5
    Last Post: 29th February 2008, 04:10
  3. convert from qstring to const char *
    By deepa.selvaraj in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2007, 12:33
  4. QString to const char
    By TheRonin in forum Newbie
    Replies: 2
    Last Post: 12th February 2007, 14:43
  5. How to convert a QString to const char *?
    By SkripT in forum Newbie
    Replies: 2
    Last Post: 10th March 2006, 10:56

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.