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. Using QDataStream with an updating file
Forum Updated to NodeBB v4.3 + New Features

Using QDataStream with an updating file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.1k Views
  • 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.
  • D Offline
    D Offline
    DRoscoe
    wrote on last edited by
    #1

    I am writing a tool that will parse a binary recording file real-time. I was planning on using QDataStream for this purpose. The intention is to open the file and initially parse as much of the file as is currently available, and then set a timer, to periodically read whatever was recorded in the interim. My question is, if I leave data in the stream and the file is written to in the meantime, will the QDataStream update to reflect the current state of the file, in particular bytesAvailable when I next query it?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Depends if the QIODevice does that, if you are using QFile it should be fine

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      D 1 Reply Last reply
      1
      • VRoninV VRonin

        Depends if the QIODevice does that, if you are using QFile it should be fine

        D Offline
        D Offline
        DRoscoe
        wrote on last edited by
        #3

        @VRonin yes, the QIODevice is a QFile

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4
          #include <QApplication>
          #include <QTimer>
          #include <QFile>
          #include <QDataStream>
          #include <QTime>
          #include <QDebug>
          int main(int argc, char* argv[])
          {
              QApplication a(argc, argv);
              QFile readFile("test.dat");
              QFile writeFile("test.dat");
              if (!writeFile.open(QIODevice::WriteOnly))
                  Q_ASSERT(false);
              if (!readFile.open(QIODevice::ReadOnly))
                  Q_ASSERT(false);
              QDataStream readStream(&readFile);
              QDataStream writeStream(&writeFile);
              QTimer writeTimer;
              writeTimer.setInterval(3000);
              QTimer readTimer;
              readTimer.setInterval(500);
              readTimer.setSingleShot(true);
              QObject::connect(&writeTimer, &QTimer::timeout, [&writeStream, &readTimer]()->void {
                  const QTime currTime = QTime::currentTime();
                  qDebug() << "Saving: " << currTime;
                  writeStream << currTime;
                  qDebug() << "bytesAvailable: " << writeStream.device()->bytesAvailable();
                  readTimer.start();
              });
              QObject::connect(&readTimer, &QTimer::timeout, [&readStream]()->void {
                  qDebug() << "bytesAvailable: " << readStream.device()->bytesAvailable();
                  QTime currTime;
                  readStream >> currTime;
                  qDebug() << "Loading: " << currTime;
              });
              QTimer::singleShot(0, &writeTimer, SLOT(start()));
              QTimer::singleShot(10000, &writeTimer, SLOT(stop()));
              return a.exec();
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          D 1 Reply Last reply
          2
          • VRoninV VRonin
            #include <QApplication>
            #include <QTimer>
            #include <QFile>
            #include <QDataStream>
            #include <QTime>
            #include <QDebug>
            int main(int argc, char* argv[])
            {
                QApplication a(argc, argv);
                QFile readFile("test.dat");
                QFile writeFile("test.dat");
                if (!writeFile.open(QIODevice::WriteOnly))
                    Q_ASSERT(false);
                if (!readFile.open(QIODevice::ReadOnly))
                    Q_ASSERT(false);
                QDataStream readStream(&readFile);
                QDataStream writeStream(&writeFile);
                QTimer writeTimer;
                writeTimer.setInterval(3000);
                QTimer readTimer;
                readTimer.setInterval(500);
                readTimer.setSingleShot(true);
                QObject::connect(&writeTimer, &QTimer::timeout, [&writeStream, &readTimer]()->void {
                    const QTime currTime = QTime::currentTime();
                    qDebug() << "Saving: " << currTime;
                    writeStream << currTime;
                    qDebug() << "bytesAvailable: " << writeStream.device()->bytesAvailable();
                    readTimer.start();
                });
                QObject::connect(&readTimer, &QTimer::timeout, [&readStream]()->void {
                    qDebug() << "bytesAvailable: " << readStream.device()->bytesAvailable();
                    QTime currTime;
                    readStream >> currTime;
                    qDebug() << "Loading: " << currTime;
                });
                QTimer::singleShot(0, &writeTimer, SLOT(start()));
                QTimer::singleShot(10000, &writeTimer, SLOT(stop()));
                return a.exec();
            }
            
            D Offline
            D Offline
            DRoscoe
            wrote on last edited by
            #5

            @VRonin Thanks... that's exactly what I was hoping for!

            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