gpt4 book ai didi

c++ - 字节数组和 QString 转换的问题

转载 作者:行者123 更新时间:2023-11-28 05:05:15 26 4
gpt4 key购买 nike

我有一个服务器和一个客户端,它们通过 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 的成员。

我该怎么办?

最佳答案

根据documentation :

QString QTextCodec::toUnicode(const QByteArray &a) const

Converts a from the encoding of this codec to Unicode, and returns the result in a QString.

从上面可以看出,需要引用而不是指针。在您的情况下,您应该将其更改为:

 QString receivedText = QTextCodec::codecForMib(1015)->toUnicode(*buffer);

关于c++ - 字节数组和 QString 转换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44989685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com