Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Run Qt inside a dll (load from mfc app)
Forum Updated to NodeBB v4.3 + New Features

Run Qt inside a dll (load from mfc app)

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SoerenS Offline
    SoerenS Offline
    Soeren
    wrote on last edited by
    #1

    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;
      w->show();
      app->processEvents();
    }
    
    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.

    jsulmJ 1 Reply Last reply
    0
    • SoerenS Soeren

      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;
        w->show();
        app->processEvents();
      }
      
      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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().

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      SoerenS 2 Replies Last reply
      0
      • jsulmJ jsulm

        @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().

        SoerenS Offline
        SoerenS Offline
        Soeren
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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().

          SoerenS Offline
          SoerenS Offline
          Soeren
          wrote on last edited by
          #4

          @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();
          
                app->exec();
          }
          

          Is that possible?

          jsulmJ 1 Reply Last reply
          0
          • SoerenS Soeren

            @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();
            
                  app->exec();
            }
            

            Is that possible?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Soeren You can use http://doc.qt.io/qt-5.7/qmetaobject.html#invokeMethod to call slots living in GUI thread from other threads.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            SoerenS 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Soeren You can use http://doc.qt.io/qt-5.7/qmetaobject.html#invokeMethod to call slots living in GUI thread from other threads.

              SoerenS Offline
              SoerenS Offline
              Soeren
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved