Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

I am porting a standalone application to plugin (VST2). Should I use QCoreApplication?



  • Hi!

    We have an existing standalone application which uses QT, that I am now porting to a C++ VST 2.4 plugin (Steinberg VST plugin, for audio processing).

    The standalone application has a QCoreApplication instance, on which it calls ->exec().

    If I don't add a QCoreApplication at all, I get a lot of errors like QSocketNotifier: Can only be used with threads started with QThread.

    But if I call ->exec() on the QCoreApplication instance, the program freezes there when debugging, never advancing past the call. Which of course makes sense, there is no need to start another "main event loop" inside of a plugin, which should be slaved to the event loop of the host calling its methods at the right moments.

    So, in this plugin, what should the role of QCoreApplication be? Is there an alternative to it I should use in this case?

    OR should I use QCoreApplication, and call exec, from within a separate thread I spawn in my plugins initialization, to keep the event loop going there without blocking?

    Thanks you in advance!
    Ilias


  • Moderators

    @IliasElk said in I am porting a standalone application to plugin (VST2). Should I use QCoreApplication?:

    The host will not be a QT application

    If the host is not a Qt application, then you can create a QCoreApplication in a new thread, no problem. See https://stackoverflow.com/questions/22289423/how-to-avoid-qt-app-exec-blocking-main-thread/22290909#22290909

    There are a few rules you must follow:

    • The QCoreApplication must be the very first QObject that you create. The thread that creates the QCoreApplication will be registered as Qt's "main thread".
      • This means you cannot use QThread, because QThread is a QObject. You must use std::thread instead.
    • You are allowed to quit/destroy your QCoreApplication object, and create a new instance later. However, you must do this in the exact same thread as the first QCoreApplication.
      • This means you cannot unload the plugin and load it again. If you do, you will get the "WARNING: QApplication was not created in the main() thread."

    which does not assume QT.

    The rules are even stricter than this. The host must not use Qt. If the host creates a QCoreApplication, then your plugin cannot create another QCoreApplication.



  • ...Calling "exec()" within a thread spawned from the class constructor gives this message:

    QCoreApplication::exec: Must be called from the main thread

    ...or, if I also create it in the run() method:

    WARNING: QApplication was not created in the main() thread.


  • Lifetime Qt Champion

    Hi,

    Which OS are you on ?



  • @SGaist

    I omitted that, sorry - Linux, currently Ubuntu 20.04 LTS, Using Qt version 5.12.8.

    Still, when creating and starting QCoreApplication in an std::thread (yesterday it was in a QThread), I get:
    WARNING: QApplication was not created in the main() thread.

    Thanks!


  • Moderators

    @IliasElk said in I am porting a standalone application to plugin (VST2). Should I use QCoreApplication?:

    there is no need to start another "main event loop" inside of a plugin, which should be slaved to the event loop of the host calling its methods at the right moments.

    Does the host program use Qt? This detail will determine how you should set up your plugin.



  • @JKSH

    The host will not be a QT application, no, the goal is that the plugin can be loaded in any software supporting the VST plugin API, which does not assume QT.

    Thanks!


  • Moderators

    @IliasElk said in I am porting a standalone application to plugin (VST2). Should I use QCoreApplication?:

    The host will not be a QT application

    If the host is not a Qt application, then you can create a QCoreApplication in a new thread, no problem. See https://stackoverflow.com/questions/22289423/how-to-avoid-qt-app-exec-blocking-main-thread/22290909#22290909

    There are a few rules you must follow:

    • The QCoreApplication must be the very first QObject that you create. The thread that creates the QCoreApplication will be registered as Qt's "main thread".
      • This means you cannot use QThread, because QThread is a QObject. You must use std::thread instead.
    • You are allowed to quit/destroy your QCoreApplication object, and create a new instance later. However, you must do this in the exact same thread as the first QCoreApplication.
      • This means you cannot unload the plugin and load it again. If you do, you will get the "WARNING: QApplication was not created in the main() thread."

    which does not assume QT.

    The rules are even stricter than this. The host must not use Qt. If the host creates a QCoreApplication, then your plugin cannot create another QCoreApplication.



  • @JKSH

    Thank you, great to know!

    Thankfully I do not know of all that many applications that can host VST plugins which use the QT framework, but that's a crucial limitation to be aware of regardless.

    Best,
    Ilias B.


Log in to reply