PDA

View Full Version : Reading from QByteArray



Misenko
29th September 2008, 23:44
Hi.

I ve got a plugin in which a write some data into the socket:



...
array = new QByteArray();
QDataStream stream(array, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_4_2);

QString name = "Program Viewer";
stream << quint32(Data::PluginData) << name << quint32(ProgramViewerMenu::NewImage) << shot.toImage();
screen = shot.toImage();

return array;
...

socket->write(array);


And in another program I have slot read() which is connected to readyRead() signal of socket.



void Connection::read()
{
DataThread *thread = new DataThread(socket, this);

connect(thread, SIGNAL(serverData(QByteArray)), this, SLOT(serverData(QByteArray)));
connect(thread, SIGNAL(pluginData(QByteArray)), this, SLOT(pluginData(QByteArray)));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );

thread->start();
}


DataThread run() function:



void DataThread::run()
{
QByteArray array;
array = socket->readAll();

QDataStream streamOut(array);
streamOut.setVersion(QDataStream::Qt_4_2);

quint32 dataType;
streamOut >> dataType;
qDebug() << "data thread dataType" << dataType;
/* QString name;
streamOut >> name;

qDebug() << name;*/

if (dataType == Data::ServerData)
emit serverData(array);
if (dataType == Data::PluginData)
emit pluginData(array);
}


pluginData signal of DataThread is connected to pluginData slot. In this slot I want to read plugin name which was written in plugin function.



void Connection::pluginData(QByteArray array)
{
QDataStream streamOut(array);
streamOut.setVersion(QDataStream::Qt_4_2);

QString name;
streamIn >> name;

qDebug() << name;
}


qDebug() << name gives me nothing, null string but I have no idea why. When I tray to read plugin name in thread class before returning array it works. Whats wrong?

Sorry about me english.

Misenko
30th September 2008, 17:17
Please. Nobody knows whats wrong? :confused: