[英]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
将此编解码器的编码从a转换为Unicode,并以QString返回结果。
从上面可以看出,需要引用而不是指针。 在您的情况下,您应该将其更改为:
QString receivedText = QTextCodec::codecForMib(1015)->toUnicode(*buffer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.