C++ 字符到 QString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14270760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:10:45  来源:igfitidea点击:

Char to QString

c++qtqstring

提问by moesef

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?

这不起作用(我收到编译器错误)。有什么建议?

采纳答案by Yuan

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=. There is no constructor for char, so your code can not compile.

但是在您的代码中,它将调用构造函数,而不是 operator=。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]));

回答by Jherson Sazo

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 。

回答by user1610015

How did you declare inBuffer? If you meant outBuffer, drop the dereference operator:

你是如何声明inBuffer 的?如果您的意思是outBuffer,请删除取消引用运算符:

QString str = outBuffer[1];