Results 1 to 6 of 6

Thread: QString to char*

  1. #1
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default QString to char*

    Hi,

    How can I convert QString to char*. I need to use char*, since my development SDK supports only char* for the particular API.

    Please do help.

    Thanks

    GIGIN JOSE

  2. #2
    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: QString to char*

    Like this:
    Qt Code:
    1. QString test = "test test test";
    2. const char *converted = test.toLatin1().data();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to char*

    You may also want to check out the declaration qPrintable.

    Xav.

  4. #4
    Join Date
    Feb 2010
    Location
    New delhi
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString to char*

    Hi Gigin Jose,

    If U work with different languages like(korea,china,thai )
    Better to use like below in commersial embedded applications in Linux plat form.

    QString strName("Test string Data");
    char * chrStr = strName.toLocal8Bit().data();
    Kotha Bhaskar
    Lingam pally

  5. #5
    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: QString to char*

    Storing the result of
    Qt Code:
    1. char *dangling_pointer = strName.toLocal8Bit().data()
    To copy to clipboard, switch view to plain text mode 
    and variants is dangerous because data() returns a pointer to a temporary QByteArray's data. That QByteArray goes out of scope at the end of the assignment statement leaving a dangling pointer. If you cannot use the intermediate value immediately then best to make the QByteArray explicit. Either:
    Qt Code:
    1. res = somefunc(strName.toLocal8Bit().data());
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QByteArray ba = strName.toLocal8Bit();
    2. // intervening stuff
    3. res = somefunc(ba.data());
    To copy to clipboard, switch view to plain text mode 
    should be safe (but I am fairly new to the C++ game and happy to be corrected).

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

    numbat (11th June 2010)

  7. #6
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to char*

    qPrintable(your QString)

Similar Threads

  1. QString to char *
    By barrygp in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2009, 20:02
  2. QString to char*
    By peace_comp in forum Qt Programming
    Replies: 4
    Last Post: 27th May 2008, 07:08
  3. QString to Char
    By kenny_isles in forum Newbie
    Replies: 1
    Last Post: 23rd February 2007, 09:51
  4. QString -> char*
    By scwizard in forum Newbie
    Replies: 4
    Last Post: 21st February 2007, 19:55
  5. char* from QString
    By bruccutler in forum Newbie
    Replies: 16
    Last Post: 14th February 2007, 06:36

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.