将QString转换为char * [英] Converting QString to char*

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

问题描述


可能重复:

QString to char conversion


我有一个函数(fopen in STL )在我的计算机中给出一个char *参数作为路径,但我必须使用QString在那个地方,所以它不工作。



如何转换QString char *以解决此问题?

解决方案

请参阅如何将QString转换为char *,反之亦然?


将QString转换为
char *,那么你首先需要得到一个
latin1表示的字符串by
调用toLatin1(),它将
返回一个QByteArray。然后在QByteArray上调用data()
,得到指向
的数据,存储在字节数组中的数据。请参阅
文档:



http:/ /qt.nokia.com/doc/qstring.html#toLatin1
http://qt.nokia.com/doc/qbytearray.html#data



有关
演示的示例如下:

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

请注意,在调用之前必须存储
bytearray
上的数据(),像下面的调用

  const char * c_str2 = str2.toLatin1()。数据(); 


会使应用程序崩溃,因为
QByteArray未存储,
因此不再存在



要将char *转换为QString,
可以使用QString构造函数,
需要一个QLatin1String,例如:

  QString string = QString(QLatin1String(c_str2)); 

请参阅文档:



http://qt.nokia.com/doc/qlatin1string.html




当然,我发现有另一种方法从这以前的答案

  QString qs; 

//如果你在任何地方使用UTF-8,那么这一点。
std :: string utf8_text = qs.toUtf8()。constData();

//或者如果你在Windows上: - )
std :: string current_locale_text = qs.toLocal8Bit()。constData();


Possible Duplicate:
QString to char conversion

I have a function (fopen in STL) that gives a char* argument as a path in my computer, but I must use QString in that place so it doesn't work.

How can I convert QString to char* to solve this problem?

解决方案

See here at How can I convert a QString to char* and vice versa?

In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array. See the documentation:

http://qt.nokia.com/doc/qstring.html#toLatin1 http://qt.nokia.com/doc/qbytearray.html#data

See the following example for a demonstration:

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

Note that it is necessary to store the bytearray before you call data() on it, a call like the following

const char *c_str2 = str2.toLatin1().data();

will make the application crash as the QByteArray has not been stored and hence no longer exists

To convert a char* to a QString you can use the QString constructor that takes a QLatin1String, e.g:

QString string = QString(QLatin1String(c_str2)) ;

See the documentation:

http://qt.nokia.com/doc/qlatin1string.html

Of course, I discovered there is another way from this previous SO answer:

QString qs;

// Either this if you use UTF-8 anywhere
std::string utf8_text = qs.toUtf8().constData();

// or this if you on Windows :-)
std::string current_locale_text = qs.toLocal8Bit().constData();

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

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