PDA

View Full Version : How to properly subclass QIODevice -- understanding the docs



hackerNovitiate
30th December 2011, 00:32
I'd like to learn how to correctly subclass a QIODevice, and I'm not sure how to interpret some sentences in the Class Reference (http://developer.qt.nokia.com/doc/qt-4.8/qiodevice.html) page:


Under pos()
"The current read/write position of the device is maintained internally by QIODevice, so reimplementing this function is not necessary. When subclassing QIODevice, use QIODevice::seek() to notify QIODevice about changes in the device position."Before reading this, I thought that seek() was to be called externally (i.e. outside the class/object) to tell the (random-access) device to move its internal pointer.

How do I use seek() to "notify QIODevice about changes"? Am I to call seek() internally as well (from member functions), and then use only seek() (and no other technique) to manipulate my internal pointer to the data?



Under seek()
"When subclassing QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's built-in buffer."
It's not clear to me: The start of which function? The external function that just opened my device?


Thanks in advance for your guidance!

ChrisW67
1st January 2012, 07:55
QIODevice keeps a track of the number of bytes read (i.e. the pos()) and allows you to skip about with seek(). If your subclass implements a seek() override that does something special, or some other function that affects the current position, it should call QIODevice::seek() so the base class is updated properly. That is:

bool MyIODevice::seek ( qint64 pos ) {
QIODevice::seek(pos);
// Do your sub-class magic with pos here
// e.g. if you need to issue commands to hardware to rewind to the corresponding position
}

If you do not override the base class seek() then you don't need to do anything special.