I was trying to convert a QString to char* type by the following methods, but they don\'t seem to work.
//QLineEdit *line=new QLineEdit();{just to describe w
Maybe
my_qstring.toStdString().c_str();
or safer, as Federico points out:
std::string str = my_qstring.toStdString();
const char* p = str.c_str();
It's far from optimal, but will do the work.
EDITED
this way also works
QString str ("Something");
char* ch = str.toStdString().C_str();
the Correct Solution Would be like this
QString k;
k = "CRAZYYYQT";
char ab[16];
sprintf(ab,"%s",(const char *)((QByteArray)(k.toLatin1()).data()) );
sprintf(ab,"%s",(const char *)((QByteArray)(k.toStdString()).data()));
sprintf(ab,"%s",(const char *)k.toStdString().c_str() );
qDebug()<<"--->"<<ab<<"<---";
Well, the Qt FAQ says:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLocal8Bit();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}
So perhaps you're having other problems. How exactly doesn't this work?