Results 1 to 8 of 8

Thread: How to convert QString to const char*

  1. #1
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    5

    Default How to convert QString to const char*

    Can anyone tell me how to convert QString to const char*?

    for example

    QString mystring;

    I tried

    mystring.data(); mystring.toLatin1(); mystring.toAscii();

    none of them work.

  2. #2
    Join Date
    Sep 2009
    Location
    Targu Mures, Romania
    Posts
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to const char*

    I just tried:

    Qt Code:
    1. QString a("Asdf");
    2. const char *c = a.toAscii();
    To copy to clipboard, switch view to plain text mode 

    It seems to work.

  3. #3
    Join Date
    May 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: How to convert QString to const char*

    You can use qPrintable macro.

  4. #4
    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 const char*

    Quote Originally Posted by fulin View Post
    Qt Code:
    1. mystring.data(); // returns QChar*
    2. mystring.toLatin1(); // returns QByteArray
    3. mystring.toAscii(); // returns QByteArray
    To copy to clipboard, switch view to plain text mode 
    none of them work.
    They all work, but they are not designed to give you a char* (const or otherwise). You want to read the relevant parts of the QByteArray docs and:
    Qt Code:
    1. char *str1 = mystring.toLatin1().data();
    2. char *str2 = mystring.toAscii().data();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt3 Qt4

    Default Re: How to convert QString to const char*

    simply use
    QString l_sStr = "Hello";
    const char* test = l_sStr.toLatin1().data(); // in Qt4
    const char* test = l_sStr.tolatin1();//in Qt3l

  6. #6
    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: How to convert QString to const char*

    Most of what is said in this thread is wrong, guys. I'll give you a hint - storing a pointer to data from a temporary object may lead to a crash.
    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.


  7. #7
    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 const char*

    Been there, done that, got the T-Shirt. Watched the application create a temporary QByteArray, grab the pointer to the constant data of the QByteArray and then the QByteArray was destroyed before the next line of code was executed, thus the pointer was no longer valid. Random crashes and garbage data from that point on. Now I'm very care about such things.

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

    Quote Originally Posted by wysota View Post
    Most of what is said in this thread is wrong, guys. I'll give you a hint - storing a pointer to data from a temporary object may lead to a crash.
    D'oh! At least that's all I was going to say before the ten character reply limit kicked in

Similar Threads

  1. convert from unsigned char* to QString
    By bhaskar in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2010, 06:36
  2. convert from qstring to const char *
    By deepa.selvaraj in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2007, 12:33
  3. How to convert QString to char *
    By rajeshs in forum Qt Programming
    Replies: 7
    Last Post: 27th September 2007, 10:32
  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.