mawia mawia - 2 months ago 27
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();
strcpy(str,ba.data());


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

Answer

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 = ba.data(); 
 printf("str2: %s", c_str2);
 return app.exec();
}

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