Important: Please read the Qt Code of Conduct -

Run Qt inside a dll (load from mfc app)

  • Hello there
    We start porting our program from mfc to qt. This cannot be done at once. Thus the simple idea is to port it step by step using a dll, that offers the qt framwork and can be used to display modal and non modal dialogs. All interface calls are done from the same thread.

    What's the best solution for handling the Qt GUI framework in a non blocking way? I want to initilaze the QtApplication and start the event loop in a start()-function and stop this loop in a stop()-function.

    • As I can see, there was some kind of QMfcApp long time ago, but I think that was some porting class?

    • Another solution that I found was:

    int * argc = nullptr;
    char ** argv = nullptr;
    QApplication * app = nullptr;
    MainWindow * win = nullptr;
    static void startup() {
      argc = new int(1);
      argv = new char*[1];
      argv[0] = strdup("dummy");
      auto app = new QApplication(*argc, argv);
      auto w = new MainWindow;
    static void shutdown()
      delete win;
      delete app;
      free argv[0];
      delete [] argv;
      delete argc;

    I don't know I there are addtional bottlenecks of not using QApplication::exec().

    • list itemThe third idea is to spawn and std::thread that handles alle GUI stuff. Means: every GUI exection happens inside that thread. The only problem here is to pipe the function calls into that thread :-(

    But I'm sure I'm not the first one with that problem and it would be nice to see what the community thinks what would be the best way to handle that nasty problem.

  • Lifetime Qt Champion

    @Soeren Without QApplication::exec() your Qt code will not work. You could move that code to a thread, allocate QApplication and everything else Qt related there and execute QApplication::exec().

  • This post is deleted!

  • @jsulm I see. But how to manage the cross thread function calls? I think I have to implement some slots inside the GUI thread that receive the GUI functions calls, haven't I?

    void thread_function()
          QApplication app(...);
          QFunctionReceiver f();

    Is that possible?

  • Lifetime Qt Champion

    @Soeren You can use to call slots living in GUI thread from other threads.

  • @jsulm Thanks a lot! I'll give it a try... ;-)

Log in to reply