How make QIODevice::writeData work asynchronously?

  • Hello all,

    I am developing app that record audio using QAudioInput and then pass it to to my class witchis inherited from QIODevice. I have reimplemented the QIODevice::writeData method. There I make my memcopy and calculations. Problem is when I launch the app it is working but GUI is stucked. I tought the QIODevice should work implicitly in different thread. I dont know how to solve this problem. Thank you for help.

    Best regards, fpartl,

  • Moderators

    @fpartl said in How make QIODevice::writeData work asynchronously?:


    No, it does not work in a different thread. You would need to move writing to file into a different thread.

  • Ok. Can you please tell me how to move my QIODevice to another thread?

  • Lifetime Qt Champion


    The QObject::moveToThread function might be what you need.

  • Thank you for reply. I dont know if I should move to thred whole class with QAudioInput a QIODevice objects or just QIODevice. I will try something.

  • Lifetime Qt Champion

    Usually you move the whole object, don't forget to parent things correctly to have everything moved properly.

  • I have done the work. I have created the the AudioWorker class witch inherits QObject. Then I maked QAudioInput and QIODevice in here and I make a forward of signals from QIODevice to catch calculated data. I moved AudioWorker to QThread and pressed the Run button. But program crash on segmentation fault in QIODevice even if I comment the whole code in writeData method.

  • Lifetime Qt Champion

    Without the code and the backtrace, it's pretty much Crystal Ball Debugging so here goes: did you check that you have everything initialised properly before using ?

  • Ou, I am sorry. The code is on GitHub. I wrote the AudioWorker class and program fails after I close the MainWindow with

    The program has unexpectedly finished.
    C:\Users\frant\QtProjects\build-PocketEar-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\PocketEar.exe crashed.

    In the AudioWorker there is instances of QAudioInput and QIODevice(MFCCPerformer).
    In the debug mode I got segmentation fault after MainWindow close on line 28 in AudioWorker (audioInput->suspend()). I think I am wrong with a closing of the thread. Thank for help.

  • Lifetime Qt Champion

    Do you have a complete stack trace ?

    Note that you are doing a double delete with audioInput and mfcc. Both of them have a parent so they will be delete when AudioWorker gets deleted.

  • Here is a stack trace after failure. I really appreciate your help. I am new in this problematics.

    Stack trace

  • I figured it out! serverStateChanged call pause method on alredy destroyed object. Now it works. What a thing! Thank you for help! :-)

  • Lifetime Qt Champion

    You're welcome !

    Since you have found the error, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

Log in to reply