Results 1 to 11 of 11

Thread: How to convert QString to LPCSTR??

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to convert QString to LPCSTR??

    Qt Code:
    1. if (Status == DEVICESTATUS_DOWNLOAD_PROGRESS) {
    2. QString out;
    3. out.sprintf(("Downloading.... %d Sectors"), (unsigned int) Param);
    4. pThis->HlpSetStatus((LPCSTR)out.utf16());
    To copy to clipboard, switch view to plain text mode 

    Am using this code but the status just displays only "D".. Please help me to resolve this issue.
    Last edited by Gokulnathvc; 25th March 2011 at 07:00.

  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 convert QString to LPCSTR??

    You are casting a "const ushort *" from QString::utf16() to a "const char *" (i.e. Long Pointer Constant STRing). Can you see the obvious disconnect here?

    Perhaps you wanted QString::toAscii(), QString::toLocal8Bit() or QString::toLatin1() combined with the const form of QByteArray::data()

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to LPCSTR??

    Still, I cannot understand this. Please help with my code

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to convert QString to LPCSTR??

    Your trying to squeeze a 16-bit array into an 8-bit array - it's not going to fit particularly well.

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to convert QString to LPCSTR??

    Thanks for your co-operation. I have solved it

  6. #6
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: How to convert QString to LPCSTR??

    Quote Originally Posted by Gokulnathvc View Post
    Qt Code:
    1. if (Status == DEVICESTATUS_DOWNLOAD_PROGRESS) {
    2. QString out;
    3. out.sprintf(("Downloading.... %d Sectors"), (unsigned int) Param);
    4. pThis->HlpSetStatus((LPCSTR)out.utf16());
    To copy to clipboard, switch view to plain text mode 

    Am using this code but the status just displays only "D".. Please help me to resolve this issue.
    Btw you shouldn't be using sprintf(). What's wrong with QString str = QString("Foo %1").arg(QString::number(param)) or QString str = "Foo " + QString::number(param)?
    Also, I recommend you use C++ reinterpret_cast, etc as C style casting will not warn you and you will have trouble finding the problem in the end.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  7. #7
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to convert QString to LPCSTR??

    how did you solve this issue

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to convert QString to LPCSTR??

    Quote Originally Posted by ComaWhite View Post
    Btw you shouldn't be using sprintf(). What's wrong with QString str = QString("Foo %1").arg(QString::number(param)) or QString str = "Foo " + QString::number(param)?
    Also, I recommend you use C++ reinterpret_cast, etc as C style casting will not warn you and you will have trouble finding the problem in the end.
    There's nothing wrong with sprintf; it's perfectly valid C and C++ code. It has the further advantage - uncovered on a recent multi-platform project - of being considerably more stable than the STL equivalent stringstream classes, which insisted on producing different output on different machines while sprintf produced exactly the same, clearly documented output.

    There's no reason not to use it, and in some cases you'll be better off. Just because something is shiny and new doesn't necessarily mean it's better, or that it even works.

  9. #9
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: How to convert QString to LPCSTR??

    Quote Originally Posted by SixDegrees View Post
    There's nothing wrong with sprintf; it's perfectly valid C and C++ code. It has the further advantage - uncovered on a recent multi-platform project - of being considerably more stable than the STL equivalent stringstream classes, which insisted on producing different output on different machines while sprintf produced exactly the same, clearly documented output.

    There's no reason not to use it, and in some cases you'll be better off. Just because something is shiny and new doesn't necessarily mean it's better, or that it even works.
    Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that uses QTextStream:
    Read the documentation on it.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to convert QString to LPCSTR??

    The problem with any method which uses varargs (not just sprintf) is that the parameters are not type safe. If the format list does not match the parameters exactly, you can be using a %s and accidentally pass it an integer causing no compiler errors*, but a segmentation fault or access violation at runtime. This is typically more of a problem when you come back later to modify code and forget about changing one or the other. It also makes things more difficult to translate into multiple languages.

    If your coming from a C background, then using the arg method may seem strange, so its perfectly natural to expect people to want to stick with methods they are more familiar with, such as sprintf. Once they have had a few such calls spit out garbage or crash there application, they'll search for a better alternative. Don't try and force such new things on them.

    * - some compilers can actually match your format specifier to the argument list and warn you if they don't match (eg passing an integer to a string specifier)

  11. #11
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to convert QString to LPCSTR??

    Again, the problem we had was different formatting on different platforms, produced by the same code. sprintf may not conform to OO, encapsulated design, but it is standardized and has been through the wringer of testing for several decades now. Output was rock-solid across all the platforms we were required to deliver to, and all others we tested.

    Theory is all very nice, but results are what matter in practice.

Similar Threads

  1. Converting a QString to a LPCTSTR?
    By dobedidoo in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2009, 14:27
  2. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  3. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 08:07
  4. How to convert Int to QString in QT4?
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 11th March 2007, 06:58
  5. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 09:00

Tags for this Thread

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.