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. 如何通过Request.ServerVariables["HTTP_USER_AGENT"]获取客户端操作系统信息

    http://www.useragentstring.com/pages/api.php

  2. Linux的/etc/issue、/etc/issue.net和/etc/motd的区别

    Linux使用这三个文件/etc/issue./etc/issue.net和/etc/motd 来控制本地及远程登录前后的信息显示,网上很多相互转载,说的都不清楚,自己实际测试了一下,结果记录如下: ...

  3. 使用 phpMyAdmin无法登录mysql的问题

    今天使用使用phpmyadmin时出现了以下错误: (1)第一次时: 当配置文件config.inc.php里的配置项是: $cfg['Servers'][$i]['host'] = 'localho ...

  4. Linux定时任务crontab命令使用详解

    1.crontab功能介绍: crontab的功能是在一定的时间间隔内定时执行一些命令. 2.crontab参数详解: 1 crontab -u //设定某个用户的cron服务,一般root用户在执行 ...

  5. 转 jQuery(图片、相册)插件代码实例

    jQuery想必大部分前端er都知道甚至很熟悉了,网上有数以万计的优秀的jQuery插件以及教程,今天收集了一些关于图片.相册的jQuery插件代码,希望会对你有所帮助. 1. 3D Gallery ...

  6. python爬虫程序

    http://blog.csdn.net/pleasecallmewhy/article/details/8922826 此人的博客关于python爬虫程序分析得很好!

  7. map(int, ..) 与 int() 的区别

    >>> map(int,') [0] >>> int('-1') -1 >>> map(int, l[1]) Traceback (most re ...

  8. iOS 摇一摇的实现-备用

    - (void)viewDidLoad { [super viewDidLoad]; [[UIApplication sharedApplication] setApplicationSupports ...

  9. 转:Durandal快速入门

    Durandal是一个轻量级的JavaScript框架,其目标是单页面应用(SPAs)的开发变得简单而优雅.它支持MVC.MVP和MVVM等模式,因此不论你采用哪种类型的前端架构,Durandal都能 ...

  10. C++代码覆盖率工具Coverage Validator

    市面上的C++代码覆盖率工具大都收费,Coverage Validator也不例外.Coverage Validator应该少有人听过,我也是在stackoverflow里听别人介绍的.所以下载了试用 ...