Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclassing QFile causes problems

Subclassing QFile causes problems

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 310 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by ocgltd
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ocgltdO Offline
        ocgltdO Offline
        ocgltd
        wrote on last edited by ocgltd
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved