QString转换成char * 的时候,一定要定义一个QBateArray的变量。不能连写

How can I convert a QString to char* and vice versa ?(trolltech)
Answer:
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:
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)) ;

Qt QString to char*的更多相关文章

  1. Qt QString转char[]数组

    Qt QString转char[]数组 QString s1="1234456";char str[20]={0};strcpy(str,s1.toStdString().c_st ...

  2. [Qt] QString 和 char* 转换

    (1) QString 转 char* char acResult[10240]; //QByteArray baResult = strResult.toLatin1(); QByteArray b ...

  3. QT QString与char *之间的转换 【转载】

    原文网址:http://blog.csdn.net/candyliuxj/article/details/6429208 1.QString转char * 先将QString转换为QByteArray ...

  4. QT QString转char*,char*转QString;简单明了,看代码。

    //原始QStringQString qs = QString::fromLocal8Bit("我的");std::string strQs = qs.toStdString(); ...

  5. Qt下 QString转char*

    Qt下面,字符串都用QString,确实给开发者提供了方便.Qt再使用第三方开源库时,由于库的类型基本上都是标准的类型,字符串遇的多的就是Char*类型 Qt再使用第三方开源库时,由于库的类型基本上都 ...

  6. Qt下QString转char*

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  7. Qt下 QString转char*(转)

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  8. Qt中 QString 转 char*

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  9. Qt中将QString转换为char *或者相反

    1.将QString转换为std::string,可以通过QString的成员函数toStdString() QString Qstr="123";std::string str= ...

随机推荐

  1. 记一次T-SQL查询优化 索引的重要性

    概述 在一次调优一个项目组件的性能问题时,发现SQL的设计真的是非常的重要,所以写一篇博文来记录总结一下. 环境介绍 这个项目组件是一个Window服务,内部在使用轮循机会在处理一个事件表中的事件,将 ...

  2. SQL 2008R2 日期转换

    --SQL CONVERT日期转换 print GETDATE() --相同 Select CONVERT(varchar(100), GETDATE(), 0) AS NDateTime --: 0 ...

  3. 中文man帮助安装

    下面我们来安装下中文man帮助 首先在http://pkgs.fedoraproject.org/repo/pkgs/man-pages-zh-CN/manpages-zh-1.5.2.tar.bz2 ...

  4. postgres安装 以及修改postgres 密码

    #postgres安装 apt-get install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 postgresql-s ...

  5. codevs 1061 重复子串

    题目描述 Description 某电视台在每一个星期天都有一个福利彩票节目,在该节目中有一个考察幸运观众记忆力的节目.节目的安排是这样的:首先由节目主持人说出一串诸如“左1右2左2左3右4左1”的数 ...

  6. Express 学习记录

    1. Express 4.0以上的版本需要独立安装 the express "generator",即 npm install -g express-generator.

  7. CSS浮动属性Float详解

    什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在 ...

  8. Android 解决listview中checkBox错位选择

    假如ListView,分成2页(或者设置数据可以纵向拉,可隐藏),每页3条数据,每个Listview的Item 里面有个checkBox,现在,当我选择第一页的前两天数据,翻到第二页,竟然第二页后两条 ...

  9. android ids.xml资源的使用

    ids.xml文件例子: XML file saved at res/values/ids.xml: 使用方式: 一:

  10. 几种开源SIP协议栈对比OPAL,VOCAL,sipX,ReSIProcate,oSIP

    随着VoIP和NGN技术的发展,H.323时代即将过渡到SIP时代,在H.323的开源协议栈中,Openh323占统治地位,它把一个复杂而又先进 的H.323协议栈展现在普通程序员的眼前,为H.323 ...