Quote Originally Posted by d_stranz View Post
So when you write to the QBuffer instance, that is when the readyRead signal is emitted. Your slot can then retrieve the QByteArray and read the new contents.
I tried a little example
qt-buffer.tar.gz

Qt Code:
  1. class Dialog : public QDialog
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. explicit Dialog(QWidget *parent = 0);
  7. ~Dialog();
  8.  
  9. private:
  10. Ui::Dialog *ui;
  11.  
  12. QBuffer *m_buffer;
  13. QByteArray *m_bytearray;
  14.  
  15. public slots:
  16. void onClick(void);
  17. void onDataReady(void);
  18. };
  19.  
  20.  
  21. Dialog::Dialog(QWidget *parent) :
  22. QDialog(parent),
  23. ui(new Ui::Dialog)
  24. {
  25. ui->setupUi(this);
  26.  
  27. m_bytearray = new QByteArray;
  28. m_buffer = new QBuffer(m_bytearray);
  29. m_buffer->open(QIODevice::ReadWrite);
  30.  
  31. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onClick()));
  32. connect(m_buffer, SIGNAL(readyRead()), this, SLOT(onDataReady()));
  33. }
  34.  
  35. Dialog::~Dialog()
  36. {
  37. delete m_buffer;
  38. delete m_bytearray;
  39. delete ui;
  40. }
  41.  
  42. void Dialog::onClick(void)
  43. {
  44. qDebug() << "write in buffer";
  45. m_buffer->write("hallo");
  46. }
  47.  
  48. void Dialog::onDataReady(void)
  49. {
  50. ba = m_buffer->readAll();
  51. qDebug() << "read" << ba.size() << "bytes";
  52. }
To copy to clipboard, switch view to plain text mode 

when write() is called the readyRead() actually emitted.
But once in the onDataReady(), the call to readAll() returns an empty bytearray, though in m_bytearray is present the string "hallo".

It seems that it is positioned to the end of the bytearray. I tried to modify:
Qt Code:
  1. void Dialog::onClick(void)
  2. {
  3. qDebug() << "write in buffer";
  4. m_buffer->write("hallo");
  5. m_buffer->reset();
  6. }
To copy to clipboard, switch view to plain text mode 
This works, but the second time I click the button it read a string "hallohallo", the third I read "hallohallohallo", an so on...

what is the proper way to read only bytes written in onClick()?

best regards
max