Qt : Convert QString to char

Qt : Convert QString to char

QString have very much feature compared with char data type. But, in some of conditions, we need to Qt : convert QString to char datatype. This is a sample function how Qt : Convert QString to char datatype :

const char *QString2char(QString str)
{
	QByteArray byteArray;
	const char *cstr;

	byteArray = str.toUtf8();
	cstr = byteArray.constData();

	return(cstr);
}

This is the sample method, how we can use Qt : Convert QString to char function above :

QString mystring;
const char *char_data;

mystring = "this is a string";
char_data = QString2char(mystring);

What do you think? Are you think this Qt : Convert QString to char is very simple 🙂

Add a Comment

Your email address will not be published. Required fields are marked *