Yes you were right. When I pass QDataStream like pointer error disappeared. But it didnt solve the problem. It didnt read 1 and not even 28 but 0.
Any idea whats wrong?
Yes you were right. When I pass QDataStream like pointer error disappeared. But it didnt solve the problem. It didnt read 1 and not even 28 but 0.
Any idea whats wrong?
let's trade: code for help?
Of course sorry:
I didnt change writing to the slot only reading from it.
Qt Code:
QByteArray array; array = socket->readAll(); quint32 dataType; *streamOut >> dataType; qDebug() << "data thread dataType" << dataType; if (dataType == Data::PluginData) emit pluginDataIn(streamOut); ... ...To copy to clipboard, switch view to plain text mode
Qt Code:
{ QString name; *streamOut >> name; qDebug() << name; quint32 type; *streamOut >> type; qDebug() << type; Menu *menu = menus.value(name); if(!menu) { qDebug() << "return menu"; return; } menu->readData(streamOut);To copy to clipboard, switch view to plain text mode
Qt Code:
{ if (!isListening) return; window->getData(stream); showWindow(); } { quint32 data; *streamOut >> data; qDebug() << "viewer window data" << quint32(data); switch (data) { case ViewerWindow::NewImage : setImage(streamOut); break; case ViewerWindow::Pixels : setPixels(streamOut); break; case ViewerWindow::Stop : clearImage(); emit closing(); break; }To copy to clipboard, switch view to plain text mode
I hope it helps.
But I will be pleased if it could be do in other way than passing QDataStream*. Maybe somehow use QByteArray or another class I dont know.
a) Did you check that your readAll() really did read all the data you sent? (It reads the available data. It is not guaranteed that all you sent is available at once and/or in a single read!). You do need to fix that!
Calling readAll the way you do is not correct: see the fortune client example in the Qt docs:network-fortuneclient-client-cpp The function readClient() 'waits' with processing a message til the complete block has been read. The server sends -as said earlier- the size of the block as the first quint32 of each block.
(Make sure by printing the size of the amount of bytes read.)
b) why don't you wrap the socket in a QDataStream directly, rather than reading into a QByteArray and wrapping that?
Other than esp. a) your code looks basically correct (it is not complete, so obviously there might be hidden issues).
At what point does your code fail (which >> does not read the expected value)?
HTH
I also noticed that you write quint32, QString, quint32 and then shot.toImage().
Qt Code:
streamOut << quint32(Data::PluginData) << pluginName << quint32(ProgramViewerMenu::NewImage) << shot.toImage();To copy to clipboard, switch view to plain text mode
But when you read, you read quint32, QString, quint32 and quint32.
Does shot.toImage() returns quint32?
to yuriry: sorry that was my mistake. It should be:
Qt Code:
{ QString name; *streamOut >> name; qDebug() << name; /* quint32 type; *streamOut >> type; qDebug() << type;*/ Menu *menu = menus.value(name); if(!menu) { qDebug() << "return menu"; return; } menu->readData(streamOut); }To copy to clipboard, switch view to plain text mode
It should read quint32, QString, quint32 and QImage. shot is a QPixmap and returning QImage.
I tried to read second quint32 in Plugins::dataIn and it works, quint32 was read correctly, but when I comment this part of code like here and call menu->readData(streamOut); it was read incorrectly in ViewerWindow::getData and I do not know why becouse I only pass the QDataStream* or QByteArray which I tried to pass before.
to caduel: I know I should write size of block first and I will repaire it.
But I know that all data which I write is read because when I read it in one class it read all data corretly but when I pass QDataStream* or QByteArray to another class, in this case from class Plugins to class ProgramViewerMenu and than ViewerWindow, it doesnt read it correctly.
I was wondering if isnt problem that classes ProgramViewerMenu and ViewerWindow are in plugin, in dll file which is loaded when program starts.
And it doesnt work even if I wrap the socket in QDataStream directly i ve just tried it.
Could the problem occur because QByteArray is on the stack and QDataStream is on the heap:
Qt Code:
QByteArray array; array = socket->readAll();To copy to clipboard, switch view to plain text mode
I was wondering that may be when you emit a signal the slot is not called right away but somehow delayed and executed through event mechanism. Then ByteArray goes out of scope and the data that QDataStream refers to become invalid. This is a just a guess, but I would also try this code to double check:
Qt Code:
*array = socket->readAll();To copy to clipboard, switch view to plain text mode
No it didnt work. Any other Ideas?
Yeah, I read up on signals - my suggestion was useless because you use a direct connection and all signals emitted on such connections are executed without any delay. In addition, QByteArray is implicitly shared...
I do not see anything obvious to suggest. May be only trying this code:
Qt Code:
To copy to clipboard, switch view to plain text mode
But it'll probably change nothing.
Well I ve tried to read directly from socket and pass QTcpSocket* and it works. But it doesnt work correctly.
I think its becouse I write to socket very quickly and also I havent added that data size control yet. But still I do not think that it is a good idea to pass socket.
If think there could be problem if i write some data before all data which was written first are read. Am I wrong?
And can me somebody explain why it works with QTcpSocket and doesnt with QByteArray?
As Caduel pointed out readAll() might not had read all the data that you sent, only the data currently available on the socket.
Try putting a sleep before reading the data from the socket and check readBufferSize() (if it is not 0 then call setReadBufferSize(0) to make it unlimited).QByteArray QIODevice::readAll ()
Reads all available data from the device, and returns it as a QByteArray.
This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.
So I cant use QByteArray in any way? And why? I still do not understant why it works with QTcpSocket and doesnt with QByteArray
And can me somebody show some piece of code how to correctly write to socket and read from socket? In my case I think I write to socket faster than it could be read from it. Is it possible? And how to do it right?
Thanks to all.
Bookmarks