QString到char *转换 [英] QString to char* conversion

查看:136
本文介绍了QString到char *转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过以下方法将QString转换为char *类型,但它们似乎不起作用。

  // QLineEdit * line = new QLineEdit(); {只是为了描述这里的行} 

QString temp = line-> text();
char * str =(char *)malloc(10);
QByteArray ba = temp.toLatin1();
strcpy(str,ba.data());你可以用这个方法来阐述可能的缺陷,或者给出一个替代方法吗?

$

b $ b

解决方案

好吧, Qt常见问题说:

  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();
}

所以也许你还有其他问题。这到底是什么工作?


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?

解决方案

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?

这篇关于QString到char *转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆