简体   繁体   English

字符到 QString

[英]Char to QString

I have a char array and want to convert one of the values from char to qstring:我有一个 char 数组,想将其中一个值从 char 转换为 qstring:

unsigned char inBuffer[64];

....
QString str= QString(*inBuffer[1]);
ui->counter->setText(str);

This isn't working (I get a compiler error).这不起作用(我收到编译器错误)。 Any suggestions?有什么建议么?

Please check http://qt-project.org/doc/qt-4.8/qstring.html请检查http://qt-project.org/doc/qt-4.8/qstring.html

QString &   operator+= ( char ch )

QString &   operator= ( char ch )

You can use operator+= to append a char, or operator= to assign a char.您可以使用 operator+= 附加一个字符,或使用 operator= 分配一个字符。

But in your code it will call constructor, not operator=.但是在您的代码中,它将调用构造函数,而不是 operator=。 There is no constructor for char, so your code can not compile. char 没有构造函数,因此您的代码无法编译。

QString str;
str = inBuffer[1];

QString has a constructor QString 有一个构造函数

QString ( QChar ch )

So u can use following code to do that所以你可以使用以下代码来做到这一点

QString str= QChar(inBuffer[1]);

or要么

QString str(QChar(inBuffer[1]));

if ur var is: char * p = "hi there";如果你的 var 是: char * p = "hi there";

You just QString myGreet = QString::fromLocal8Bit(p);你只是QString myGreet = QString::fromLocal8Bit(p);

How did you declare inBuffer ?你是如何声明inBuffer 的 If you meant outBuffer , drop the dereference operator:如果您的意思是outBuffer ,请删除取消引用运算符:

QString str = outBuffer[1];

This is the easiest way to do that:这是最简单的方法:

QString x="";
QChar y='a';

x+=y;

So here you have a QString with the char.所以这里你有一个带有字符的 QString 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM