【问题标题】:problems with QByteArray and QString convertionQByteArray 和 QString 转换的问题
【发布时间】:2017-12-12 20:50:01
【问题描述】:

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

    }
}

last Line 在服务器控制台打印客户端输入的文本。我想将缓冲区转换为 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 的成员。

我该怎么办?

【问题讨论】:

    标签: c++ qt qt5


    【解决方案1】:

    根据documentation

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

    将此编解码器的编码转换为 Unicode,并返回 产生一个 QString。

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2016-10-14
      • 2012-07-02
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多