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. Writing to the file continuously in Qt (realtime data)?

Writing to the file continuously in Qt (realtime data)?

Scheduled Pinned Locked Moved Solved General and Desktop
qfilebluetooth low e
5 Posts 3 Posters 2.0k 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.
  • S Offline
    S Offline
    SpaceToon
    wrote on last edited by
    #1

    Hi,

    I am building a GUI application with Qt which takes sensor data in realtime from a sensor (via Bluetooth LE) and writes it to a file.

    I have a function, which transforms the raw data coming from the sensor and saves these data as key-value pairs in a map. This function is a slot and is called every time new data is available (~every second). So, after transforing these data, I want to call a function writeDatraToFile which takes this map and writes the values in a txt. file. I have this approac so far:

    void Data::transformDataSlot(QString newSensorValue){
    
    //make some calculations and save this into a map     QMap<QString, float> sensorData;
    
    wirteDataToFile(directory,fileName, sensorData);
    }
    
    void Data::wirteDataToFile(QString directory, QString fileName, QMap<QString, float> sensorData)
    {
    
    /* Try and open a file for output */
        
            QFile file(directory.replace('\\', '/') + "/" + fileName + ".txt");
            file.open(QIODevice::Append);
        
        
            /* Check it opened OK */
            if(!outputFile.isOpen()){
            qDebug() <<"- Error, unable to open" << outputFilename << "for output";
            return ;
            }
    
            /* Point a QTextStream object at the file */
            QTextStream outStream(&outputFile);
    
            /* Write the line out of the map the file */
            outStream <<"test \n";
    
            /* Close the file */
        
            file.close();
    }
    

    For this approach, I have read:

    Note that opening the file for every single line is an inefficient solution, especially if you have many, many lines to write. >Opening the file once, then writing all the words to it before closing it, would work faster.

    So at first, I would say that I will delete the file.close() statement out of this function. Then I will connect a Slot to my Button "save" in my GUI and call "file.close" in this slot. Would this work? But what if an exception occurs while writing to the file or e.g. the application crashes or the user accidently quits the application without clicking on save. How can I make sure, that my file will still close? Maybe this?

    try:
        // writes to file
    finally:
        file.close()
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      It does not help if you don't call close() since it's closed when the object goes out of scope.
      Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        It does not help if you don't call close() since it's closed when the object goes out of scope.
        Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).

        S Offline
        S Offline
        SpaceToon
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in Writing to the file continuously in Qt (realtime data)?:

        It does not help if you don't call close() since it's closed when the object goes out of scope.
        Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).

        Thank you, I did not know that.
        So my code looks like this now:

        //in header file I have QFile file;
        
        //in my .cpp:
        void Data::wirteDataToFile(QString directory, QString fileName, QMap<QString, float> sensorData)
        {
        
        
                file.setFileName(directory.replace('\\', '/') + "/" + fileName + ".txt");
                file.open(QIODevice::Append);
            
                if(!outputFile.isOpen()){
                qDebug() <<"- Error, unable to open" << outputFilename << "for output";
                return ;
                }
        
                QTextStream outStream(&outputFile);
        
                /* Write the line out of the map the file */
                outStream <<"test \n";
        
        }
        
        

        Then, I can call close, if my Button "save" is clicked.
        One further question:

        Since it is a continuous writing, that means that the file has to be opened jus once and the filename has to be set just once too. So I thought to have a flag called "firstTime" and every time check if firstTime is true (means that the file is not set or opened) then set the name and open otherwise do not. Are my considerations correct?:

        if (firstTime){
                file.setFileName(directory.replace('\\', '/') + "/" + fileName + ".txt");
                file.open(QIODevice::Append);
            }
        
        //after writing and so on 
        
        firstTime = false;
        
        mrjjM 1 Reply Last reply
        0
        • S SpaceToon

          @Christian-Ehrlicher said in Writing to the file continuously in Qt (realtime data)?:

          It does not help if you don't call close() since it's closed when the object goes out of scope.
          Make it a member of your class and call flush() after every write to make sure it's written to disk (as good as the OS supports this).

          Thank you, I did not know that.
          So my code looks like this now:

          //in header file I have QFile file;
          
          //in my .cpp:
          void Data::wirteDataToFile(QString directory, QString fileName, QMap<QString, float> sensorData)
          {
          
          
                  file.setFileName(directory.replace('\\', '/') + "/" + fileName + ".txt");
                  file.open(QIODevice::Append);
              
                  if(!outputFile.isOpen()){
                  qDebug() <<"- Error, unable to open" << outputFilename << "for output";
                  return ;
                  }
          
                  QTextStream outStream(&outputFile);
          
                  /* Write the line out of the map the file */
                  outStream <<"test \n";
          
          }
          
          

          Then, I can call close, if my Button "save" is clicked.
          One further question:

          Since it is a continuous writing, that means that the file has to be opened jus once and the filename has to be set just once too. So I thought to have a flag called "firstTime" and every time check if firstTime is true (means that the file is not set or opened) then set the name and open otherwise do not. Are my considerations correct?:

          if (firstTime){
                  file.setFileName(directory.replace('\\', '/') + "/" + fileName + ".txt");
                  file.open(QIODevice::Append);
              }
          
          //after writing and so on 
          
          firstTime = false;
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @SpaceToon
          hi
          Yes you just want to open it once.
          But i dont see you calling flush anywhere ?

          S 1 Reply Last reply
          1
          • mrjjM mrjj

            @SpaceToon
            hi
            Yes you just want to open it once.
            But i dont see you calling flush anywhere ?

            S Offline
            S Offline
            SpaceToon
            wrote on last edited by
            #5

            @mrjj said in Writing to the file continuously in Qt (realtime data)?:

            @SpaceToon
            hi
            Yes you just want to open it once.
            But i dont see you calling flush anywhere ?

            Oh, I forgot to paste the flush here, it is actually in my code directly after the write-statement. Thank you very much :)

            1 Reply Last reply
            1

            • Login

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