QString 到 char* 的转换
我试图通过以下方法将 QString 转换为 char* 类型,但它们似乎不起作用.
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?
推荐答案
好吧,Qt常见问题说:
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?
相关文章