简体   繁体   中英

problems with QByteArray and QString convertion

I've got a server and a client and they're connected by TCP (QTcpSocket and QTcpServer). data is sent using 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 prints the text entered in client in server's console. i want to convert buffer to QString. (or i want to send it to qml file). so when i try :

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

and give me the error :

no matching function for call to 'QTextCodec::toUnicode(QByteArray*&)'
         receivedText = QTextCodec::codecForMib(1015)->toUnicode(buffer);
                                                     ^

when using fromAscii or fromStringC it says it's not a member of QString.

what should i do?

According to the 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.

From the above, it follows that the reference is needed and not the pointer. In your case you should change it to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM