Results 1 to 17 of 17

Thread: how to conver QString to const char*

  1. #1
    Join Date
    Mar 2011
    Location
    delhi
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation how to conver QString to const char*

    hi all
    how can we convert QString to const char*


    thanks with regards:
    gauravg

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to conver QString to const char*

    A camel can go 14 days without drink,
    I can't!!!

  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*

    or QString::toAscii().data()
    ==========================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.

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to conver QString to const char*

    Quote Originally Posted by high_flyer View Post
    or QString::toAscii().data()
    one should never do that.

    it should be
    Qt Code:
    1. QByteArrary ba = s.toAscii();
    2. const char* p = ba.constData();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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*

    one should never do that.
    do what?
    Your code is "doing" what I suggested.
    You are using the const variant, it depends on your needs if you need the const or not.
    ==========================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.

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to conver QString to const char*

    Just remember that QString::toAscii().data() involves use of temporary object (of QByteArrary), so in next line this pointer is not valid anymore. That is why nish has given more safe solution.

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to conver QString to const char*

    Quote Originally Posted by high_flyer View Post
    do what?
    Your code is "doing" what I suggested.
    You are using the const variant, it depends on your needs if you need the const or not.
    its not about const. It about temporary object. Qt docs has mentioned to avoid this mistake.(Read the QByteArray docs)

  8. #8
    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*

    its not about const. It about temporary object. Qt docs has mentioned to avoid this mistake.(Read the QByteArray docs)
    Between avoid making a mistake (which means understand what implicit sharing is) to "one should never do that." there is a big difference.
    If YOU read the docs for QByteArray::data() you will see, that they don't say anything about NOT using it.
    They do say however:
    The pointer remains valid as long as the byte array isn't reallocated or destroyed. For read-only access, constData() is faster because it never causes a deep copy to occur.

    This function is mostly useful to pass a byte array to a function that accepts a const char *.
    So you definitively CAN and SHOULD use that for cases where it is needed - provided you READ the documentation, and aware of the pitfalls.
    There are ofte several possibles to do the same thing - and one should look at the best suited option.

    People, you should really stop with these "NEVER DO <something> OR THE WORLD WILL END" assertions.
    The methods are there to be used, just make sure you understand what you are doing when you use them and what the consequences are.
    So relax.
    ==========================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.

  9. #9
    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*

    I'll quote the Qt docs again with emphasis:
    The pointer remains valid as long as the byte array isn't reallocated or destroyed.
    and add some from the C++ standards:
    $12.2/3- "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception."
    So, at the statement boundary of:
    Qt Code:
    1. char *str = s.toAscii().data();
    To copy to clipboard, switch view to plain text mode 
    (s is QString) the temporary QByteArray created by the toAscii() method is destroyed. Most of the time immediately subsequent access through str will appear to work because the memory has not yet been allocated elsewhere and modified. You can be sure when it does fail intermittently you will be scratching your head. If you are going to store a pointer to the char data for later use then you must control the scope of the QByteArray by making it explicit in some form (which is what was suggested).

    That does not, in any way, prohibit this immediate usage:
    Qt Code:
    1. printf("%s\n", s.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 
    which is safe because the scope of the temporary QByteArray is the end of the complete statement.

  10. #10
    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.

  11. #11
    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.

  12. #12
    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.

  13. #13
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to conver QString to const char*

    @high_flyer
    i think your reply to me has been replied, so there is no point i start flaming like you. so i will not answer your full post. but here it goes

    Quote Originally Posted by high_flyer View Post
    People, you should really stop with these "NEVER DO <something> OR THE WORLD WILL END" assertions.
    agreed. if i would have given the full explanation before, then things should have not gone this far.

    Quote Originally Posted by high_flyer View Post
    So relax.
    ok me relaxing. But read the rules of the internet, "all caps" are considered as screaming/shouting/flaming, so looks like the 'wiseguy' need some relaxing.


    EDIT:-
    and yes i just typed the constData() coz i have a habbit of using this function, it dosent matter in our OP case wether we use data() or constData(),
    Last edited by nish; 15th April 2011 at 12:16.

  14. #14
    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*

    so there is no point i start flaming like you.
    I was not flaming - and if it was perceived as such, then I am sorry.
    "all caps" are considered as screaming/shouting/flaming,
    true, and I meant it this way too.
    But please note the quotes "" - this is not something I am saying but others - I am quoting others - in this case you - I capitalized to make it a bit more extreme - but even with out capitalizing saying "never do ... !" is maybe not shouting but close.
    Ergo - since it was not me shouting, I was relaxed.
    And - a 'wiseguy' is not a wise guy - as in smart, rather it is meant more as a negative term - its a slang word for mafia members, trouble maker.

    P.S
    At times (and in recent times even more) I tend to react a bit harsh, the reason is, that there are a LOT of posts which are really stupid.
    Sometimes, I misjudge a post, and give it cold shower when it was not quite called for.
    But you will not find serious posts treated this way by me, and I have no problem saying sorry or that I made a mistake if I am proven wrong.
    Last edited by high_flyer; 15th April 2011 at 13:05.
    ==========================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.

  15. #15
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to conver QString to const char*

    Quote Originally Posted by high_flyer View Post
    I was not flaming - and if it was perceived as such, then I am sorry.
    no problems .

    But please note the quotes "" - this is not something I am saying but others - I am quoting others - in this case you - I capitalized to make it a bit more extreme - but even with out capitalizing saying "never do ... !" is maybe not shouting but close.
    lets forget it. If i reply then you will reply then i will ... we will trap ourselves in a circular list!

    Ergo - since it was not me shouting, I was relaxed.
    looks like we both are in spa.

    And - a 'wiseguy' is not a wise guy - as in smart, rather it is meant more as a negative term - its a slang word for mafia members, trouble maker.
    here in India we use this term for a wise guy only.
    P.S
    At times (and in recent times even more) I tend to react a bit harsh, the reason is, that there are a LOT of posts which are really stupid.
    Sometimes, I misjudge a post, and give it cold shower when it was not quite called for.
    we all were stupids(newbies) once!! Just ignore the posts

    But you will not find serious posts treated this way by me, and I have no problem saying sorry or that I made a mistake if I am proven wrong.
    Thats the great thing about you!.

    Peace out.

  16. #16
    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*

    ere in India we use this term for a wise guy only.
    Based on your answer I figured as much.

    we all were stupids(newbies) once!! Just ignore the posts
    Just to make it clear: being newbie != posting stupid.
    There are newbie posts which are legitimate.
    Many even.
    But some just rather post and get spoon fed instead of first searching, investing some thought or simple search on google/docs - and at least try to get to the answer by them selves - resulting in a stupid post.
    The forum is here for asking and discussing Qt related issues, newbies SHOULD post and ask.
    But when you post show that you have invested at least some effort in finding the solution. (I don't mean you, but as a figure of peach).
    ==========================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.

  17. #17
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to conver QString to const char*

    Quote Originally Posted by high_flyer View Post
    Just to make it clear: being newbie != posting stupid.
    and thats why i wrote newbie in brackets().

    There are newbie posts which are legitimate.
    Many even.
    But some just rather post and get spoon fed instead of first searching, investing some thought or simple search on google/docs - and at least try to get to the answer by them selves - resulting in a stupid post.
    The forum is here for asking and discussing Qt related issues, newbies SHOULD post and ask.
    But when you post show that you have invested at least some effort in finding the solution. (I don't mean you, but as a figure of peach).
    yup.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.