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. Implement and handle life time of QCoreApplication. How to?
Forum Updated to NodeBB v4.3 + New Features

Implement and handle life time of QCoreApplication. How to?

Scheduled Pinned Locked Moved Unsolved General and Desktop
websocketqcoreappthreadquestionsfeedback
4 Posts 2 Posters 124 Views 1 Watching
  • 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.
  • G Offline
    G Offline
    GyroPower
    wrote last edited by GyroPower
    #1
    • Qt version: 6.9
    • Using Arch Linux OS
    • Modules downloaded from Arch repos

    I want to know how to handle QCoreApplications objects life time and implement it on my project for Websockets and custom rendering with GLFW-OpenGL and ImGui for the users input and chat like experience.

    Currently using Boost::Beast for my websocket client
    I want to use QWebsockets for later compile the client to web assembly and put it on a web app made too in C++ as the client side.

    I want to see if I can implement Qt websocket echo client example simple, but some times gets buggy in the way the example is present, It hangs some times the runtime of the compile binary and even cant reboot my pc, gets hang and needs to be shut down by force.

    How I handle and Shut down QCoreApplications and handle multi threats or implement it on std::threats?

    Is it possible to do it simple?

    My code have two threats besides the main threat, one for the i/o context for the websocket beast client and one for rendering.

    How I handle the lifetime of the QCoreApplication object?.

    // myGui contains the actual Gui structure
    #include"myGui.hpp"
    //This is where is initialize GLFW and ImGui
    #include"windowContext.hpp"
    //The header for the simple websocket echo client example
    #include"wb_session_qt.hpp"
    #include<QtCore/QObject>
    //The header for the websocket client of Boost::Beast
    #include"wb_session.hpp"
    
    
    int main(int argc, char *argv[]) {
       QCoreApplication Qt_wb_app_manager(argc, argv);
       
       //The Gui have other related code with hot code reloading but it does not concern my question so is simplified code
       windowContext window_context;   
       Igui gui;
    
       boost::asio::io_context ioc;
       //Own session is the Beast websocket client
       auto ws_client = std::make_shared<own_session>(ioc, userName, shared_chatHistory);
       ws_client->connect(host, port);
       
       std::thread io_thread([&ioc] { ioc.run(); });
        //Echo_Client is the WebSocket Echo Client example of Qt 
       bool debug = true;
       Echo_Client client(QUrl(QStringLiteral("ws://127.0.0.1:8080")), debug);
       QObject::connect(&client, &Echo_Client::closed, &Qt_wb_app_manager, &QCoreApplication::quit);
       Qt_wb_app_manager.exec();
    
        /*  what I do with the QCoreApplication object? What if I only execute once
             the example and shut down the object and the client, and  what if I want
             to keep it alive and pass it to the gui to send messages on users input on
             the Gui? How can I do it?
            */
       std::thread render_thread([&gui, &ws_client, &client, &Qt_wb_app_manager] 
            {
                // initialize GLFW and ImGui for rendering
                window_context.m_init(1280,720,"websocket client");
                
                // initialize ImGui context for the Gui code
                gui->m_init( ws_client,window_context.m_getImGuiContext());
            
                //put the respecting Gui object, this is for code reloading but
                //is not put here, not relevant
                window_context.m_setGui(gui);
            
                while(!window.should_stop)
                {
                    
                    
                    window_context.m_render();
                    
    
                    
                }
            
                
        });
    
            
        
        render_thread.join();
        ioc.stop();
        io_thread.join();
        window_context.m_shutdown();
    
    }
    

    If I can refer to some article or guide or maybe some one can help to understand I will appreciate it a low

    jsulmJ 1 Reply Last reply
    0
    • G GyroPower
      • Qt version: 6.9
      • Using Arch Linux OS
      • Modules downloaded from Arch repos

      I want to know how to handle QCoreApplications objects life time and implement it on my project for Websockets and custom rendering with GLFW-OpenGL and ImGui for the users input and chat like experience.

      Currently using Boost::Beast for my websocket client
      I want to use QWebsockets for later compile the client to web assembly and put it on a web app made too in C++ as the client side.

      I want to see if I can implement Qt websocket echo client example simple, but some times gets buggy in the way the example is present, It hangs some times the runtime of the compile binary and even cant reboot my pc, gets hang and needs to be shut down by force.

      How I handle and Shut down QCoreApplications and handle multi threats or implement it on std::threats?

      Is it possible to do it simple?

      My code have two threats besides the main threat, one for the i/o context for the websocket beast client and one for rendering.

      How I handle the lifetime of the QCoreApplication object?.

      // myGui contains the actual Gui structure
      #include"myGui.hpp"
      //This is where is initialize GLFW and ImGui
      #include"windowContext.hpp"
      //The header for the simple websocket echo client example
      #include"wb_session_qt.hpp"
      #include<QtCore/QObject>
      //The header for the websocket client of Boost::Beast
      #include"wb_session.hpp"
      
      
      int main(int argc, char *argv[]) {
         QCoreApplication Qt_wb_app_manager(argc, argv);
         
         //The Gui have other related code with hot code reloading but it does not concern my question so is simplified code
         windowContext window_context;   
         Igui gui;
      
         boost::asio::io_context ioc;
         //Own session is the Beast websocket client
         auto ws_client = std::make_shared<own_session>(ioc, userName, shared_chatHistory);
         ws_client->connect(host, port);
         
         std::thread io_thread([&ioc] { ioc.run(); });
          //Echo_Client is the WebSocket Echo Client example of Qt 
         bool debug = true;
         Echo_Client client(QUrl(QStringLiteral("ws://127.0.0.1:8080")), debug);
         QObject::connect(&client, &Echo_Client::closed, &Qt_wb_app_manager, &QCoreApplication::quit);
         Qt_wb_app_manager.exec();
      
          /*  what I do with the QCoreApplication object? What if I only execute once
               the example and shut down the object and the client, and  what if I want
               to keep it alive and pass it to the gui to send messages on users input on
               the Gui? How can I do it?
              */
         std::thread render_thread([&gui, &ws_client, &client, &Qt_wb_app_manager] 
              {
                  // initialize GLFW and ImGui for rendering
                  window_context.m_init(1280,720,"websocket client");
                  
                  // initialize ImGui context for the Gui code
                  gui->m_init( ws_client,window_context.m_getImGuiContext());
              
                  //put the respecting Gui object, this is for code reloading but
                  //is not put here, not relevant
                  window_context.m_setGui(gui);
              
                  while(!window.should_stop)
                  {
                      
                      
                      window_context.m_render();
                      
      
                      
                  }
              
                  
          });
      
              
          
          render_thread.join();
          ioc.stop();
          io_thread.join();
          window_context.m_shutdown();
      
      }
      

      If I can refer to some article or guide or maybe some one can help to understand I will appreciate it a low

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

      @GyroPower said in Implement and handle life time of QCoreApplication. How to?:

      Qt_wb_app_manager.exec();

      This is a blocking call. Execution of the code after exec() will continue when the Qt event loop started by exec() stops. In a Qt application the event loop has to run as long as the application runs. Not sure what you want to achieve. And keep in mind: your "while(!window.should_stop)" loop would block Qt event loop and make your app unresponsive. Such loops are a no-go in event driven applications.

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

      G 1 Reply Last reply
      0
      • jsulmJ jsulm

        @GyroPower said in Implement and handle life time of QCoreApplication. How to?:

        Qt_wb_app_manager.exec();

        This is a blocking call. Execution of the code after exec() will continue when the Qt event loop started by exec() stops. In a Qt application the event loop has to run as long as the application runs. Not sure what you want to achieve. And keep in mind: your "while(!window.should_stop)" loop would block Qt event loop and make your app unresponsive. Such loops are a no-go in event driven applications.

        G Offline
        G Offline
        GyroPower
        wrote last edited by
        #3

        @jsulm I want to achive make the QCoreApplication to exist with my own rendering loop, and how to handle his life time with that rendering loop for this example a websocket client made with QWebsockets and QObject, what I ask is for approach to handle this if It is possible, and you may wonder why, because is good to know the limits and see if it is possible and I would like to use the websocket client made from Qt modules to compile the whole client with the already Gui made to webassembly in immediate future, but if it is to difficult or annoying to achieve I guess I will take an implementation with emscripten sdk, I wanted in the first place to do it with Qt to have an approach with the project and maybe later use it on some projects to learn it and maybe get a job requiring these set of libraries knowledge and use, but thanks for questioning the purpose of my post.

        jsulmJ 1 Reply Last reply
        0
        • G GyroPower

          @jsulm I want to achive make the QCoreApplication to exist with my own rendering loop, and how to handle his life time with that rendering loop for this example a websocket client made with QWebsockets and QObject, what I ask is for approach to handle this if It is possible, and you may wonder why, because is good to know the limits and see if it is possible and I would like to use the websocket client made from Qt modules to compile the whole client with the already Gui made to webassembly in immediate future, but if it is to difficult or annoying to achieve I guess I will take an implementation with emscripten sdk, I wanted in the first place to do it with Qt to have an approach with the project and maybe later use it on some projects to learn it and maybe get a job requiring these set of libraries knowledge and use, but thanks for questioning the purpose of my post.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote last edited by
          #4

          @GyroPower Just create your render thread before you call Qt_wb_app_manager.exec();

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

          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