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:
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
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.
To be on the safe side use code like:
const QByteArray ba
= string.
toAscii();
// make ba const, because modifying this array might otherwise invalidate the pointer const char *sequence = ba.constData();
// now sequence will remain valid within the current scope.
const QByteArray ba = string.toAscii(); // make ba const, because modifying this array might otherwise invalidate the pointer
const char *sequence = ba.constData();
// now sequence will remain valid within the current scope.
To copy to clipboard, switch view to plain text mode
Bookmarks