Originally Posted by spud There is one gotcha with the code above! The call to toAscii() creates a temporary QByteArray which goes out of scope when used like this: Qt Code: Switch view char *sequence = string.toAscii().constData();// sequence is now a dangling pointer! char *sequence = string.toAscii().constData(); // sequence is now a dangling pointer! To copy to clipboard, switch view to plain text mode a call like Qt Code: Switch view qstrdup(str.toAscii().constData()); qstrdup(str.toAscii().constData()); To copy to clipboard, switch view to plain text mode will work, though, since the the pointer isn't accessed after the QByteArray goes out of scope. [/code] Correct, of course. I just wrote the code here, didn't tested it anywhere. I think it was easy to spot in a program because probably a memory violation would have been raised as soon as the pointer was read the first time. Regards
char *sequence = string.toAscii().constData();// sequence is now a dangling pointer!
char *sequence = string.toAscii().constData(); // sequence is now a dangling pointer!
qstrdup(str.toAscii().constData());
try the following un-tested code; QString str ="ABCDEFGHIJK"; unsigned char* uca; uca = new unsigned char[str.length()]; for(int i=0; i<str.length(); i++) uca[i] = str.substr(i,1); cout<<uca<<endl; delete [] uca; comlink21
Originally Posted by comlink21 try the following un-tested code; QString str ="ABCDEFGHIJK"; unsigned char* uca; uca = new unsigned char[str.length()]; for(int i=0; i<str.length(); i++) uca[i] = str.substr(i,1); cout<<uca<<endl; delete [] uca; comlink21 What is your point?
Forum Rules
Bookmarks