mawia mawia - 1 month ago 4x
C++ Question

QString to char* conversion

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 what is line here}

QString temp=line->text();
char *str=(char *)malloc(10);
QByteArray ba=temp.toLatin1();

Can you elaborate the possible flaw with this method, or give an alternative method?


Well, the Qt FAQ says:

int main(int argc, char **argv)
 QApplication app(argc, argv);
 QString str1 = "Test";
 QByteArray ba = str1.toLatin1();
 const char *c_str2 =; 
 printf("str2: %s", c_str2);
 return app.exec();

So perhaps you're having other problems. How exactly doesn't this work?