Results 1 to 8 of 8

Thread: How to convert QString to char *

  1. #1
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up How to convert QString to char *

    I want to use sscanf function ,

    I am using this function as follows,

    QString s=" " //Some data;

    sscanf(s,format,value1,value2 ,...);

    this shows error like can't convert QString to char *,

    So i tried ,

    (char*)(s.data()) this is returning only first characcter,

    usage like this,

    sscanf((char*)(s.data()),format,value1,value2 ,...);


    How to convert QString to char *?

    Please help me.

    Thanks,
    Rajesh.S
    Thanks,
    Rajesh.S

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

    Default Re: How to convert QString to char *

    I'd rather use for example QTextStream instead of sscanf. But if you insist, How can I convert a QString to char* and vice versa ?

    PS. QString to char* conversion has been discussed millions of times..
    J-P Nurmi

  3. #3
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to convert QString to char *

    Sorry , I searched for that i didn't get, Please post that links,

    Thanks,
    Rajesh.S
    Thanks,
    Rajesh.S

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

    Default Re: How to convert QString to char *

    What's wrong with the link to Trolltech Knowledgebase?
    J-P Nurmi

  5. #5
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to convert QString to char *

    sory in that link i didn't get clear idea.

    Thanks
    Rajesh.S
    Thanks,
    Rajesh.S

  6. #6
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to char *

    Sorry I went wrong link,.

    Now i got the correct code


    QApplication app(argc, argv);
    QString str1 = "Test";
    QByteArray ba = str1.toLatin1();
    const char *c_str2 = ba.data();
    printf("str2: %s", c_str2);
    return app.exec();


    Thank you for your valuable reply.

    Thanks,
    Rajesh.S
    Thanks,
    Rajesh.S

  7. #7
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to char *

    And what about:
    Qt Code:
    1. qString.toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 
    ?

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

    Quote Originally Posted by mtrpoland View Post
    And what about:
    Qt Code:
    1. qString.toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 
    ?
    You're asking yourself for a segfault here if you intend to ever use the resulting char*. The std::string is a temporary object that will get destroyed immediately, so such a construction is useless. If you look at the example, it avoids creating a temporary variable. But if you avoid it, you can of course go through std::string, if you have STL support compiled into Qt.

Similar Threads

  1. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 07:52
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. QString to Char
    By kenny_isles in forum Newbie
    Replies: 1
    Last Post: 23rd February 2007, 09:51
  4. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  5. QString to char
    By boss_bhat in forum Qt Programming
    Replies: 7
    Last Post: 20th July 2006, 23:26

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.