Subclassing QFile causes problems
-
I have subclassed QFile (for a simple encryption/decryption class) and have reimplemented readData and writeData. My testing shows read/write work perfectly.
Now, I am trying to serialize objects using a QDataStream, to write to my QFile subclass. It seems that calls to QFile putchar are directed to QIODevice::putchar, which sends them back file QFilePrice::putchar which then bypasses writeData, and writes directly to the device (bypassing my encryption).
This would seem to break the ability to subclass QFile. Is subclassing QFile the wrong way to do what I want?
I discovered by tracing QIODevice that if I turn off buffering at file open, then all I/O goes through writeData (and my code works)...but I'm still wondering if I'm solving this the wrong way.
-
Hi,
Internally putChar calls write which then calls writeData so your custom function should be called as well.
Did you test it by calling it yourself explicitly to see if you have the expected result ?
-
QIODevice calls to putChar (using d_func ptr) call the QFilePrivate::putchar method. And the QFilePrivate::putchar (which calls a helper) will directly write the character to the buffer, NOT call writeData. However, if I force the file to unbuffered mode then QFilePrivate::putchar will in fact call writeData.
So as a workaround I have set unbuffered mode and my code works. But this seems like a bit of a hack. Given what I'm doing with encryption, this file is acting more like a sequential device. I either write everything to the file at once, or read everything from the file at once. No seeking, no overwriting.
If I were to do this over, what should I use as the base class? I found a QFileDevice..or I suppose I could could subclass QIODevice but I'd have to write a bunch of file specific functions. I would also like to be able to set the sequential flag, which I don't think I can do in QFileDevice. I would like to learn the write way to do this.