I am having the following codes::
Qt Code:
char * size; QString str; 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
I am having the following codes::
Qt Code:
char * size; QString str; 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
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:
size = new char [str.size() + 1]; memcpy (size, qPrintable(str), str.size()); 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!!!
Gokulnathvc (20th April 2011)
It works, But am getting some unknown characters following the correct data.....
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?
what is the value of "value" ???
A camel can go 14 days without drink,
I can't!!!
did you notice this in the QString::sprintf documentation:
Then also look at QString::arg.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"
Last edited by schnitzel; 20th April 2011 at 01:31. Reason: updated contents
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).
Bookmarks