I'm using Qt5 and i'm trying to send QPixmap over QUdpProtocol with QByteArray packet in QDataStream. Send pixmap is simple (and correct?):
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_0);
out << (quint32)0;
out << pixmap;
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));
socket->write(block);
qDebug() << "Block size:" << (float)(block.size()/1024.f) << "KB";
But reading... Nothing happens - I mean, QLabel isn't filled, any change, any error message.
QByteArray buffer;
buffer.resize(sock->pendingDatagramSize());
sock->readDatagram(buffer.data(), buffer.size(), &remoteAddr, &remotePort);
if (sock->bytesAvailable() > 0)
return;
QDataStream out(&buffer, QIODevice::ReadOnly);
out.setVersion(QDataStream::Qt_5_0);
QPixmap p;
//out.device()->seek(0);
out >> p;
ui->label->setPixmap(p);
I know about packet fragmentation in UDP, but I tried sending 50 pixels, 20, 10, 5, and finally - 1. Nothing. Both programs shows same bytes count (sended and received). If this matters, I'm binded both sockets at QHostAddress::LocalHost
with same port, but raw text was sended successfully.
What I am doing wrong?
Huh, success.
Code from internet is invalid . I tried this way:
QByteArray frame;
QDataStream out(&frame, QIODevice::WriteOnly);
out << myPixmap;
QDataStream in(&frame, QIODevice::ReadOnly);
QPixmap pix2;
in >> pix2;
ui->label->setPixmap(pix2);
It works and it's easy.
On Qt5 bytes in start about size isn't needed. Also, transfer by TCP/UDP works fine (if you have troubles, remember about maximum packet size).
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.