
Originally Posted by
d_stranz
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
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
public slots:
void onClick(void);
void onDataReady(void);
};
ui(new Ui::Dialog)
{
ui->setupUi(this);
m_buffer
= new QBuffer(m_bytearray
);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onClick()));
connect(m_buffer, SIGNAL(readyRead()), this, SLOT(onDataReady()));
}
Dialog::~Dialog()
{
delete m_buffer;
delete m_bytearray;
delete ui;
}
void Dialog::onClick(void)
{
qDebug() << "write in buffer";
m_buffer->write("hallo");
}
void Dialog::onDataReady(void)
{
ba = m_buffer->readAll();
qDebug() << "read" << ba.size() << "bytes";
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QBuffer *m_buffer;
QByteArray *m_bytearray;
public slots:
void onClick(void);
void onDataReady(void);
};
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
m_bytearray = new QByteArray;
m_buffer = new QBuffer(m_bytearray);
m_buffer->open(QIODevice::ReadWrite);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onClick()));
connect(m_buffer, SIGNAL(readyRead()), this, SLOT(onDataReady()));
}
Dialog::~Dialog()
{
delete m_buffer;
delete m_bytearray;
delete ui;
}
void Dialog::onClick(void)
{
qDebug() << "write in buffer";
m_buffer->write("hallo");
}
void Dialog::onDataReady(void)
{
QByteArray ba;
ba = m_buffer->readAll();
qDebug() << "read" << ba.size() << "bytes";
}
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:
void Dialog::onClick(void)
{
qDebug() << "write in buffer";
m_buffer->write("hallo");
m_buffer->reset();
}
void Dialog::onClick(void)
{
qDebug() << "write in buffer";
m_buffer->write("hallo");
m_buffer->reset();
}
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
Bookmarks