簡體   English   中英

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

將此編解碼器的編碼從a轉換為Unicode,並以QString返回結果。

從上面可以看出,需要引用而不是指針。 在您的情況下,您應該將其更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM