toUtf8() is a QByteArray, created temporary, that's why you can't do like this:
const char *acsUserName = Qstr1.toUtf8().constData();
const char *acsUserName = Qstr1.toUtf8().constData();
To copy to clipboard, switch view to plain text mode
because at the next line acsUserName pointer will point to memory of destroyed temporary object.
you only can send this construction to a function by argument like this:
void foo( const char * userName, const char * password )
{
...
}
...
foo( Qstr1.toUtf8().constData(), Qstr2.toUtf8().constData() );
void foo( const char * userName, const char * password )
{
...
}
...
foo( Qstr1.toUtf8().constData(), Qstr2.toUtf8().constData() );
To copy to clipboard, switch view to plain text mode
or you should make new memory for this C-style string like this:
char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsUserName, Qstr1.toUtf8().constData() );
char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsPassword, Qstr2.toUtf8().constData() );
foo( acsUserName, acsPassword );
delete []acsUserName;
delete []acsPassword;
char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsUserName, Qstr1.toUtf8().constData() );
char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
memcpy( acsPassword, Qstr2.toUtf8().constData() );
foo( acsUserName, acsPassword );
delete []acsUserName;
delete []acsPassword;
To copy to clipboard, switch view to plain text mode
Bookmarks