Important: Please read the Qt Code of Conduct -

Large incoming data to QByteArray and what to do with it?

  • Hello everyone.

    I have an audio recording application that reads audio data from a microphone system. The user presses record and after an unknown duration he is done recording, he will click to save the file.

    Each second streams about 8MB of data into a qbuffer - qbytearray.
    On average, the user suggests about 15 mins worth of recording. However, it could be shorter/longer.
    For 15 mins, size = 15m * 60s * 8 M = 7.03GB. (I only managed to do 3 minutes before my pc crashed)

    1. Any suggestions on what I can do to prevent having so much data lingering in my qbytearray?

    2. Would writing directly to an open file be better?

    3. Can I keep a file stream open while I collect data before writing it over?

    4. What about temporary files? I have tried creating temporary files with the QTemporaryFile class and it gets saved to my .../temp/ folder. I don't know how how I can translate that temp file into a permanent file?


  • Lifetime Qt Champion


    1. Well, write it to a file
    2. Yes
    3. Sure
    4. Simply move the file to final location. See

  • Moderators

    QBuffer is a QIODevice. QFile is one, too. So, you can combine using QByteArray as buffer and keep saving to QFile at the same time, if you want to. I think it would be easier to just write directly to a QFile, though.

    My main suggestion is to use QDataStream to stream the data into the file. This way you won't be consuming much RAM - just enough to receive and save the audio as it arrives. This should solve both 1 & 2.

      • yes.
      • use QSaveFile instead of QTemporaryFile, if you want to. But using normal file and data stream is the way to go, I think.

    Some pseudo code to get you started:

    // init
    QDataStream stream(&file);
    // when audio comes in
    void readAudio(const QByteArray &audioPacket) {
      stream << audioPacket;

  • @sierdzio and @jsulm

    Thank you both for the suggestions. I will work on number 4 with moving of tempfiles or using a QSaveFile.