Results 1 to 6 of 6

Thread: convert char *string to QString

  1. #1
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default convert char *string to QString

    hi,

    I am new to QT. i am not getting the proper way to convert a char * string to QString that i need to display in lineEdit.

    Here is the scenario:

    I have a header file my_header.h In that i have a variable named char * string. This variable will update by some other application.

    I need to update this variable into a lineEdit that accept only QString.. Could you please tell me how i can convert that to QString.

    And I need to do reverse operation also.such as i need to get data from line edit and update in the variable in the header..


    Thanks in advance

  2. #2
    Join Date
    May 2010
    Posts
    19
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: convert char *string to QString

    Search for sprintf function in QString.

    char *data = "string";
    QString str;
    str.sprintf("%s",data);
    qDebug() << str;
    Last edited by p3c0; 18th April 2012 at 13:18. Reason: updated contents

  3. #3
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: convert char *string to QString

    you can just

    char* data = "string";
    QString str;
    str = data;

    This should work also.

  4. #4
    Join Date
    Apr 2012
    Posts
    49
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: convert char *string to QString

    Thanks..it worked for me..

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: convert char *string to QString

    sprintf is useful for other stuff
    QString has constructor that takes a char* (and, as kito said, the operator= also can take char*).

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

    You may need to take into consideration encoding of the C-style string also: is it plain ASCII, Latin 1, UTF-8 or some other supported encoding? QString has a range of static conversion functions for handling these differences if the default constructor/operator=() handling is not quite right.

  7. The following user says thank you to ChrisW67 for this useful post:

    Zlatomir (19th April 2012)

Similar Threads

  1. How to convert QString to const char*
    By fulin in forum Newbie
    Replies: 7
    Last Post: 9th May 2010, 23:25
  2. How to convert QString to std::string or char*?
    By yangyunzhao in forum Qt Programming
    Replies: 26
    Last Post: 26th August 2009, 06:52
  3. convert from qstring to const char *
    By deepa.selvaraj in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2007, 12:33
  4. How to convert QString to char *
    By rajeshs in forum Qt Programming
    Replies: 7
    Last Post: 27th September 2007, 10:32
  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.