View Full Version : Reading from QByteArray

29th September 2008, 23:44

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

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

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

return 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()) );


DataThread run() function:

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

QDataStream streamOut(array);

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);

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.

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