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. 关于ASP.Net 4.0的ClientID

    我们知道因为在原来的ASP.NET应用程序中使用服务端控件在生成ClientID的时,是很难控制的,特别是在嵌套的控件的时候,比如在多个嵌套Repeater中要控制某一个控件生成的html的ID属性, ...

  2. PHP 异常处理

    PHP 异常处理 异常用于在指定的错误发生时改变脚本的正常流程. 异常是什么 PHP 5 提供了一种新的面向对象的错误处理方法. 异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程.这种情况 ...

  3. C/C++默认浮点型

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

  4. tribonacci

    Everybody knows Fibonacci numbers, now we are talking about the Tribonacci numbers: T[0] = T[1] = T[ ...

  5. basename usage in linux

    作用:去掉文件的目录和后缀 1.去掉文件路径 jenkins@work:~/ci/script$ basename /backup/jenkins/ci/script/Release.sh.bak R ...

  6. SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)

    文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915   一.配置文件:SpringMVC 用的是 的MultipartFil ...

  7. IIS 中asp.net的一些配置

    安装了IIS之后, 添加了虚拟目录然后运行页面, 出现了一点儿错误, 好像是不认识aspx文件, 把aspx文件当成是xml文件处理. 无法显示 XML 页. 使用 XSL 样式表无法查看 XML 输 ...

  8. 在ubuntu14.14 安装php扩展扩展出现的问题

    我是在ubuntu14.14 安装的 lnmp. 部分扩展.均已安装好,但是我用apt-get 方式安装 redis和curl扩展时,我的配置都设置但是从phpinfo里面看没有响应的配置项. 于是我 ...

  9. C# 实现将PDF转文本的功能

    这篇文章最初只描述使用 PDFBox 来解析PDF文件.现在它已经被扩展到包括使用 IFilter 和 iTextSharp 的例程了.  这篇文章和对应的Visual Studio项目已经更新到目前 ...

  10. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...