Results 1 to 7 of 7

Thread: How to copy(convert) QString to char* ??

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to copy(convert) QString to char* ??

    I am having the following codes::

    Qt Code:
    1. char * size;
    2. QString str;
    3. str.sprintf("Size %s",value);
    To copy to clipboard, switch view to plain text mode 

    How to copy the str value to size??? please help me

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to copy(convert) QString to char* ??

    size is a pointer, then you cannot copy directly the contents of str in size.

    you have to allocate memory for size. For example
    Qt Code:
    1. size = new char [str.size() + 1];
    2. memcpy (size, qPrintable(str), str.size());
    3. size[str.size()] = '\0';
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    Gokulnathvc (20th April 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to copy(convert) QString to char* ??

    It works, But am getting some unknown characters following the correct data.....

  5. #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 copy(convert) QString to char* ??

    It may work, but it will also be a memory leak if you do not delete the memory after use.

    The "unknown characters" will be the toLocal8Bit() conversion of whatever was in the your string. The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding. Since we cannot see what is your string we cannot tell you what is going on.

    How do you intend to actually use the size variable after you have copied the string?

  6. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to copy(convert) QString to char* ??

    what is the value of "value" ???
    A camel can go 14 days without drink,
    I can't!!!

  7. #6
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to copy(convert) QString to char* ??

    did you notice this in the QString::sprintf documentation:

    Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that uses QTextStream:
    QString result;
    QTextStream(&result) << "pi = " << 3.14;
    // result == "pi = 3.14"
    Then also look at QString::arg.
    Last edited by schnitzel; 20th April 2011 at 01:31. Reason: updated contents

  8. #7
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: How to copy(convert) QString to char* ??

    You can assign the value in str to size with:

    QByteArray ar = str.toAscii(); // or toLatin1 or toLocal8Bit
    size = ar.data();

    However, size will be referencing the bytes in the QByteArray object and will go "poof" if/when the QByteArray is destructed.

    Otherwise, you can do:

    QByteArray ar = str.toAscii();
    size = new char[ar.count() + 1];
    memcpy(size, ar.constData(), ar.count() + 1);

    and then size will "own" its own copy of the data (and it will need to be explicitly deleted later).

Similar Threads

  1. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 09:05
  2. How to convert QString to const char*
    By fulin in forum Newbie
    Replies: 7
    Last Post: 9th May 2010, 23:25
  3. How to convert QString to std::string or char*?
    By yangyunzhao in forum Qt Programming
    Replies: 26
    Last Post: 26th August 2009, 06:52
  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

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.