Results 1 to 2 of 2

Thread: Reading from QByteArray

  1. #1
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Reading from QByteArray

    Hi.

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

    Qt Code:
    1. ...
    2. array = new QByteArray();
    3. QDataStream stream(array, QIODevice::WriteOnly);
    4. stream.setVersion(QDataStream::Qt_4_2);
    5.  
    6. QString name = "Program Viewer";
    7. stream << quint32(Data::PluginData) << name << quint32(ProgramViewerMenu::NewImage) << shot.toImage();
    8. screen = shot.toImage();
    9.  
    10. return array;
    11. ...
    12.  
    13. socket->write(array);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void Connection::read()
    2. {
    3. DataThread *thread = new DataThread(socket, this);
    4.  
    5. connect(thread, SIGNAL(serverData(QByteArray)), this, SLOT(serverData(QByteArray)));
    6. connect(thread, SIGNAL(pluginData(QByteArray)), this, SLOT(pluginData(QByteArray)));
    7. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
    8.  
    9. thread->start();
    10. }
    To copy to clipboard, switch view to plain text mode 

    DataThread run() function:

    Qt Code:
    1. void DataThread::run()
    2. {
    3. QByteArray array;
    4. array = socket->readAll();
    5.  
    6. QDataStream streamOut(array);
    7. streamOut.setVersion(QDataStream::Qt_4_2);
    8.  
    9. quint32 dataType;
    10. streamOut >> dataType;
    11. qDebug() << "data thread dataType" << dataType;
    12. /* QString name;
    13. streamOut >> name;
    14.  
    15. qDebug() << name;*/
    16.  
    17. if (dataType == Data::ServerData)
    18. emit serverData(array);
    19. if (dataType == Data::PluginData)
    20. emit pluginData(array);
    21. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void Connection::pluginData(QByteArray array)
    2. {
    3. QDataStream streamOut(array);
    4. streamOut.setVersion(QDataStream::Qt_4_2);
    5.  
    6. QString name;
    7. streamIn >> name;
    8.  
    9. qDebug() << name;
    10. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading from QByteArray

    Please. Nobody knows whats wrong?

Similar Threads

  1. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34
  2. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  3. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53
  4. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.