Results 1 to 4 of 4

Thread: QString to char *

  1. #1
    Join Date
    Dec 2008
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default QString to char *

    Why does the first assignment not work in the section of code below.. does not seem to matter whether to8bit is used as well or not.. However, works when you pass it through a string class.

    Does not work:
    QString s = QString(tr("whatever"));

    const char * asdfasdf = s.toAscii().data();


    qDebug(asdfasdf);

    ---------------
    Works:

    QString s = QString(tr("whatever"));

    std::string str = std::string(s.toAscii().data());
    const char * asdfasdf = str.c_str();


    qDebug(asdfasdf);

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QString to char *

    Quote Originally Posted by barrygp View Post
    const char * asdfasdf = s.toAscii().data();
    Left: const char *
    Right: char *

    const char * asdfasdf = str.c_str();
    Left: const char *
    Right: const char *

    See the difference?

    Qt Code:
    1. const char *xyz = s.toAscii().constData();
    To copy to clipboard, switch view to plain text mode 

    By the way, all these assignments can lead to a crash.

    You can try this:
    Qt Code:
    1. QByteArray ba = s.toAscii();
    2. const char *xyz = ba.constData(); // as long as ba exists, xyz will be valid
    To copy to clipboard, switch view to plain text mode 
    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.


  3. #3
    Join Date
    Sep 2009
    Posts
    4

    Default Re: QString to char *

    Quote Originally Posted by barrygp View Post
    Why does the first assignment not work in the section of code below.. does not seem to matter whether to8bit is used as well or not.. However, works when you pass it through a string class.

    Does not work:
    QString s = QString(tr("whatever"));

    const char * asdfasdf = s.toAscii().data();


    qDebug(asdfasdf);

    ---------------
    Works:

    QString s = QString(tr("whatever"));

    std::string str = std::string(s.toAscii().data());
    const char * asdfasdf = str.c_str();


    qDebug(asdfasdf);

    s.toAscii() create a interim variable that will be released before qDebug(asdfasdf).

    try qDebug(s.toAscii().data());

    It must work.

  4. #4
    Join Date
    Sep 2009
    Posts
    4

    Default Re: QString to char *

    Quote Originally Posted by barrygp View Post
    ---------------
    Works:

    QString s = QString(tr("whatever"));

    std::string str = std::string(s.toAscii().data());
    const char * asdfasdf = str.c_str();


    qDebug(asdfasdf);
    here you have created a std::string str and asdfasdf = str.c_str() is available at qDebug(asdfasdf).
    Last edited by zhnde; 4th September 2009 at 20:24.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  4. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 07:52
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.