Results 1 to 11 of 11

Thread: QString to "char*"

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default QString to "char*"

    Hi,

    Why my char pointer don't get the correct value?
    Qt Code:
    1. QString qName("MyName");
    2. const char* Name = qName.toAscii().data();
    3. //also tryied:
    4. //const char* Name = qName.toAscii().constData();
    To copy to clipboard, switch view to plain text mode 
    I think that I did it before using this way.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: QString to "char*"

    I am not 100% sure but qName.toAscii() creating a temporary QByteArray wich is deleted afterwards??? By caching the QByteArray it works...
    Qt Code:
    1. QString qName("MyName");
    2. QByteArray ba = qName.toAscii();
    3. const char* Name = ba.data();
    4. while (*Name) {
    5. qWarning() << *Name;
    6. ++Name;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Lykurg

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

    ^NyAw^ (23rd April 2009)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QString to "char*"

    What do you need that const char* for? You can just store the QByteArray and pass it to any function that takes a const char*, since QByteArray has const char* operator().

    The problem of yours is that you store a pointer to the internal data of a temporary QByteArray object returned by QString::toAscii(). As soon as the QByteArray object goes out of scope, which happens right after the statement, the pointer becomes invalid.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    ^NyAw^ (23rd April 2009)

  6. #4
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: QString to "char*"

    qPrintable( qName )

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: QString to "char*"

    Quote Originally Posted by lni View Post
    qPrintable( qName )
    is expanded like
    Qt Code:
    1. #ifndef qPrintable
    2. # define qPrintable(string) (string).toLocal8Bit().constData()
    3. #endif
    To copy to clipboard, switch view to plain text mode 
    So you have still the problem with the temporal QByteArray!

  8. #6
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: QString to "char*"

    Quote Originally Posted by Lykurg View Post
    is expanded like
    Qt Code:
    1. #ifndef qPrintable
    2. # define qPrintable(string) (string).toLocal8Bit().constData()
    3. #endif
    To copy to clipboard, switch view to plain text mode 
    So you have still the problem with the temporal QByteArray!
    I thought he just needs to convert QString to const char* (for output purpose?). If he needs the memory to hold the string value, the "qName" already has it...

  9. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: QString to "char*"

    Quote Originally Posted by lni View Post
    I thought he just needs to convert QString to const char* (for output purpose?). If he needs the memory to hold the string value, the "qName" already has it...
    For output ourposes qPrintable is surely the best solution, but sometimes 3rd party libraries needed a char array and therefore you have to go via the "cached" QByteArray, I think...

  10. #8
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: QString to "char*"

    Quote Originally Posted by Lykurg View Post
    For output ourposes qPrintable is surely the best solution, but sometimes 3rd party libraries needed a char array and therefore you have to go via the "cached" QByteArray, I think...
    If 3rd party libraries doesn't know QString, it won't know QByteArray either. If you pass QString reference around, I don't see how QByteArray is better. You have qPrintable as well as QString::toStdString for the conversion.

  11. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: QString to "char*"

    Quote Originally Posted by lni View Post
    If 3rd party libraries doesn't know QString, it won't know QByteArray either. If you pass QString reference around, I don't see how QByteArray is better. You have qPrintable as well as QString::toStdString for the conversion.
    Look, I don't want to quarrel with you. Your solution with qPrintabel is fine just as the expanded version which the thread starter has used: qName.toAscii().data(). And of course if 3rd party libraries doesn't know QString, it won't know QByteArray either. But if they need a const char* argument qPrintable is no solution because it is not valid for use the array as ^NyAw^ has figured out. Problem is the not "cached" QByteArray mentioned by me and jpn. So you have to create a temporary QByteArray as long as you need a valid const char* array. As long as I understand right now.
    Qt Code:
    1. QString qName("MyName");
    2. QByteArray ba = qName.toAscii();
    3. const char* Name = ba.data();
    4. const char* NameWithout = qName.toAscii().constData();
    5. // use the array somewhere...
    6. // NameWithout is not valid
    7. // Name is
    8.  
    9. // destroy ba
    10. // Name is now also invalid
    To copy to clipboard, switch view to plain text mode 

  12. #10
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QString to "char*"

    Quote Originally Posted by lni View Post
    If 3rd party libraries doesn't know QString, it won't know QByteArray either. If you pass QString reference around, I don't see how QByteArray is better. You have qPrintable as well as QString::toStdString for the conversion.
    As it was mentioned in one of previous posts, QByteArray has const char * operator, which is casting QByteArray into const char * when there is need for such a cast.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  13. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 59 Times in 57 Posts

    Default Re: QString to "char*"

    Hi,

    Sorry for delay.

    Thanks for replies.
    Catching QByteArray is a good solution. I'm able to understand why my code didn't work.
    I'm using a third party lib that needs a "const char*" as argument and I have the value stored into a QString. So, you are right when you suposed it.

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  3. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.