QByteArray 和 QString 转换的问题
problems with QByteArray and QString convertion
我有一个服务器和一个客户端,它们通过TCP(QTcpSocket和QTcpServer(连接。 数据使用 QByteArray 发送。
void Receive::newConnection()
{
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), SLOT(disconnected()));
QByteArray *buffer = new QByteArray();
qint32 *s = new qint32(0);
buffers.insert(socket, buffer);
sizes.insert(socket, s);
qDebug()<<buffer;
}
}
最后一行在服务器控制台中打印在客户端中输入的文本。 我想将缓冲区转换为 QString。(或者我想把它发送到QML文件(。所以当我尝试时:
QString receivedText = QTextCodec::codecForMib(1015)->toUnicode(buffer);
并给我错误:
no matching function for call to 'QTextCodec::toUnicode(QByteArray*&)'
receivedText = QTextCodec::codecForMib(1015)->toUnicode(buffer);
^
当使用fromAscii或fromStringC时,它说它不是QString的成员。
我该怎么办?
根据文档:
QString QTextCodec::toUnicode(const QByteArray &a( const
将此编解码器的编码转换为 Unicode,并返回 导致 QString。
综上所述,需要引用而不是指针。在您的情况下,您应该将其更改为:
QString receivedText = QTextCodec::codecForMib(1015)->toUnicode(*buffer);
相关文章:
- C++转换参数初始化问题
- 将父类对象强制转换为子类的问题
- 模板类转换问题 - 无法推断调用的函数
- 将旧管道转换为现代 openGL 时出现问题
- 将字节数组转换为带有字节序问题的指针
- 复制构造函数隐式转换问题
- 使用转换器提升 Python 问题 - 静态链接
- 与模板 (c++) 相关的转换问题
- 将文件复制到自定义位置,存在字符串转换问题
- 从基指针到派生的强制转换问题
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- PX 转换例程编译问题
- 类型转换问题
- C++,遇到将双精度转换为整数的问题
- 拆分 pybind11 模块和自动类型转换问题
- 类型转换问题:返回为整数而不是浮点/类型
- Unicode 字符问题/转换参数
- c++和OpenCV:问题转换图像到灰度