when QBuffer emits readyRead signal?
Hi
Reading QBuffer documentation:
The question is: how new data can arrive in the buffer?
I mean, I declared a QBuffer:
In my thought new data is considered arrived if I write data in the byteArray directly:
Code:
byteArray.append("hallo");
but readyRead() is never called
so how new data can arrive in the buffer to make readyRead() be emitted?
best reagards
Max
Re: when QBuffer emits readyRead signal?
I think you misunderstand how the QBuffer / QByteArray relationship works. As the docs say:
Quote:
The QBuffer class provides a QIODevice interface for a QByteArray.
...
QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file. \
By default, an internal QByteArray buffer is created for you when you create a QBuffer. You can access this buffer directly by calling buffer(). You can also use QBuffer with an existing QByteArray by calling setBuffer(), or by passing your array to QBuffer's constructor.
You read and write from the QBuffer instance, not the QByteArray. The QByteArray is just the internal storage used by the QBuffer. QBuffer allows you to treat a QByteArray as if it was a file or other random access I/O device.
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.
1 Attachment(s)
Re: when QBuffer emits readyRead signal?
Quote:
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
Attachment 11851
Code:
{
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";
}
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:
Code:
void Dialog::onClick(void)
{
qDebug() << "write in buffer";
m_buffer->write("hallo");
m_buffer->reset();
}
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
Re: when QBuffer emits readyRead signal?
You still misunderstand, I think.
When you construct a QBuffer with a QByteArray as an argument, that QByteArray is the one that will be modified when you call QBuffer::write(). Calling QBuffer::readAll() returns the internal QByteArray, not the one you gave it as the constructor argument. So of course, the internal array is empty because you told QBuffer not to use it, but to use yours instead.
So the code you wrote first will work if you simply replace this line:
Code:
m_buffer
= new QBuffer(m_bytearray
);
with this line:
When you call QBuffer::reset(), you are telling QBuffer to go back to using its internal buffer and ignore yours, which is why you get a copy of your string in that case. Each time you write, you append more data to the buffer (which is why you get multiple copies of the string).
Re: when QBuffer emits readyRead signal?
Since you have two access points I guess you will have to maintain your two positions yourself.
Compare that with QFile, which is also a QIODevice.
If you append to it, then the position information (QIODevice::pos() is the end of the file. If you start a read there, you won't get any data (you are at the end of the file after all).
So you need to remember your read and write positions, then seek to the correct one before you perform the I/O operation.
Cheers,
_
Re: when QBuffer emits readyRead signal?
It sounds like you want the buffer to contain only the most recently written bytes. I think in this case you need to do something like the following:
Code:
internal.clear();
mpBuffer->seek( 0 ); // Maybe not required; QBuffer might check the array size before writing
This should result in an empty buffer without a readyRead() signal. As the docs say:
Quote:
QByteArray & QBuffer::buffer()
Returns a reference to the QBuffer's internal buffer. You can use it to modify the QByteArray behind the QBuffer's back.