QAudioOutput constructor hangs in PyQt6

ekhumoro ekhumoro at gmx.com
Sat Apr 11 18:49:37 BST 2026


On 11/04/2026 17:27, Phil Thompson wrote:
> The attached is a version of your Python code modified so that it *is* the equivalent of the C++ code. It does not hang.
> 
> Your original code enters the event loop unconditionally, ie. when there is no audio to play

Actually, it doesn't enter the event loop *at all*, because it hangs when creating the audio output object. Your version 
of the code also hangs on my system, for exactly the same reason. The critical section is this:

     qDebug('creating audio output...')
     audio = QAudioOutput()              # <-- hangs here
     qDebug('audio output created')

With both your version and mine, the second debug statement is never printed on my system. If I use a single-shot timer, 
the event-loop is started *first*, and the `main` function is called later - which prevents the hang. But then if I 
install a qt message-handler, it starts hanging again. If you can't reproduce this, it's most likely because the 
back-end on your system doesn't produce any messages when creating the audio object (or at least not the same ones). 
Presumably, when you run my original PyQt6 example on your system, you'll get this output:

     Press Ctrl+C to quit

     qt.multimedia.ffmpeg: Using Qt multimedia with FFmpeg version n8.1 GPL version 3 or later
     creating audio output...
     # maybe some different message here...
     audio output created
     cancelled: no audio source provided

and then the code will just spin the event-loop until you press Ctrl+C - which is exactly as intended. (Obviously, in my 
real application, the main window is shown once the event-loop starts - or at least, it would be, if the code ever 
reached that point).

Given the way `qInstallMessageHandler` works, I assume PyQt6 must doing *something* different - otherwise it's hard to 
understand why the C++ example never hangs on my system. The *only* difference seems to be the handling of that specific 
warning message. If it wasn't null-terminated, or perhaps contained some unexpected control characters, could that 
affect PyQt6 somehow?

PS: I just noticed the original PyQt6 example I posted has a `qInstallMessageHandler(messageHandler)` line inside the 
main() function, which shouldn't be there - but it doesn't change anything if it's removed.


More information about the PyQt mailing list