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. C/C++默认浮点型

    代码: #include <iostream> #include <cstdio> using namespace std; void test(int a){ cout< ...

  2. jq实现鼠标经过图片翻滚效果

    短短的十多行代码就实现了一个酷炫的图片翻滚代码,要实现这个效果并不难,只要思路对了,一切都好办,不多说了,直接上代码看效果! html结构: <ul class="list" ...

  3. CFont 字体类

    CFont 字体类 初始化函数 CreateFontIndirect 初始化一个由LOGFONT结构给出其特征的CFont对象 CreateFont 初始化用指定特性定义的CFont对象 Create ...

  4. mysql 日期比较

    情景是:距离当前到期时间多少天 可以考虑当前系统时间加上某个天数后,与数据库的字段作比较 1.已知的时间>=CURDATE()+10 2.在数据的查询中,考虑到sql语句的优化问题,应减少通配符 ...

  5. 转:VmWare下安装CentOS6图文安装教程

    文章来自于:http://www.cnblogs.com/seesea125/archive/2012/02/25/2368255.html 查看文章索引请通过http://www.cnblogs.c ...

  6. Kcptun 是一个非常简单和快速的,基于KCP 协议的UDP 隧道,它可以将TCP 流转换为KCP+UDP 流

    本博客曾经发布了通过 Finalspeed 加速 Shadowsocks 的教程,大家普遍反映能达到一个非常不错的速度.Finalspeed 虽好,就是内存占用稍高,不适合服务器内存本来就小的用户:而 ...

  7. Printing Architecture

    Printing Architecture http://www.codeproject.com/Articles/8916/Printing-Architecture     This articl ...

  8. Android 利用Application对象存取公共数据

    本文章来给大家介绍Android 利用Application对象存取公共数据. Android系统在运行每一个程序应用的时候,都会创建一个Application对象,用于存储与整个应用相关的公共变量. ...

  9. vsftpd,tftp安装配置

    一. 对比共同点:都包含ftp不同点:1)vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序.你可以通过ftp客户端上传下载软件.可设置访问用户名密码,或匿名anonymous登陆.默认 ...

  10. 通过JS触发TextBox的ontextchanged事件,并获取TextBox所在GridView的那一行

    protected void txtInsNum_TextChanged(object sender, EventArgs e) { TextBox t = (TextBox)sender; Grid ...