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. Manage QEvent manual in different thread

Manage QEvent manual in different thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 662 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.
  • Q Offline
    Q Offline
    Quiccz
    wrote on last edited by
    #1

    I'm using glfw for gui, and use qt core library. Like this

    int main()
    {
        QApplication app;   // I also use QPainter for image and text. So i need QApplication in main thread
        std::thread([](){
                 while(1)
                 {
                       //render something with glfw
                 }
         }).death();
        app.exec();
    }
    

    now i want to use qevent for render thread. Like this

    int main()
    {
        QCoreApplication app;
        std::thread([](){
                 while(1)
                 {
                        QCoreApplication::ProcessEvent(); //manage event manual before every render.
                       //render something with glfw
                 }
         }).death();
        QTimer timer; // sent some event to glfw thread
        app.exec();
    }
    

    Can my goal be achieved?

    jsulmJ 1 Reply Last reply
    0
    • Q Quiccz

      I'm using glfw for gui, and use qt core library. Like this

      int main()
      {
          QApplication app;   // I also use QPainter for image and text. So i need QApplication in main thread
          std::thread([](){
                   while(1)
                   {
                         //render something with glfw
                   }
           }).death();
          app.exec();
      }
      

      now i want to use qevent for render thread. Like this

      int main()
      {
          QCoreApplication app;
          std::thread([](){
                   while(1)
                   {
                          QCoreApplication::ProcessEvent(); //manage event manual before every render.
                         //render something with glfw
                   }
           }).death();
          QTimer timer; // sent some event to glfw thread
          app.exec();
      }
      

      Can my goal be achieved?

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

      @Quiccz Why do you need a second thread?

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

      Christian EhrlicherC Q 3 Replies Last reply
      0
      • jsulmJ jsulm

        @Quiccz Why do you need a second thread?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @jsulm ... and why use std::thread when one wants to use Qt signal/slot and other functionality in there?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Quiccz Why do you need a second thread?

          Q Offline
          Q Offline
          Quiccz
          wrote on last edited by Quiccz
          #4

          @jsulm I thought qt event loop must start with exec().

          jsulmJ 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Quiccz Why do you need a second thread?

            Q Offline
            Q Offline
            Quiccz
            wrote on last edited by
            #5

            @jsulm

            int main()
            {
                QCoreApplication app;
               QTimer timer; // sent some event to glfw thread
            
                while(1)
                {
                        QCoreApplication::ProcessEvent(); //manage event manual before every render.
                        //render something with glfw
                 }
                return 0;
            }
            

            Can't believe this work.

            However, i send Event to top object, it's seem not send to child object.
            I want to create a static object as all receiver father. So i just need send event to this static object and it handle to child.
            Like this

            QObject top_object;
            QObject r1(&top_object);
            QObject r2(&top_object);
            
            PostEvent(top_object, event);
            
            //so r1 deal event first, then r2.
            
            
            1 Reply Last reply
            0
            • Q Quiccz

              @jsulm I thought qt event loop must start with exec().

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

              @Quiccz said in Manage QEvent manual in different thread:

              I thought qt event loop must start with exec().

              Yes, you have it already: app.exec()
              So, still don't know why you need a second thread.

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

              Q 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Quiccz said in Manage QEvent manual in different thread:

                I thought qt event loop must start with exec().

                Yes, you have it already: app.exec()
                So, still don't know why you need a second thread.

                Q Offline
                Q Offline
                Quiccz
                wrote on last edited by Quiccz
                #7

                @jsulm because app.exec() will block, and i need render my frame in while(1) loop. if in same thread, app.exec() will never run.

                I found QEvent can be send and recive without exec(). So i don't need a second thread. which i don't know before

                Christian EhrlicherC 1 Reply Last reply
                0
                • Q Quiccz

                  @jsulm because app.exec() will block, and i need render my frame in while(1) loop. if in same thread, app.exec() will never run.

                  I found QEvent can be send and recive without exec(). So i don't need a second thread. which i don't know before

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Quiccz said in Manage QEvent manual in different thread:

                  because app.exec() will block, a

                  app.exec() does not block anything. If you want to render something (to whereever) then do it inside the event loop e.g. with a QTimer or during the paintEvent() when you want to paint it on the screen.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  Q 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @Quiccz said in Manage QEvent manual in different thread:

                    because app.exec() will block, a

                    app.exec() does not block anything. If you want to render something (to whereever) then do it inside the event loop e.g. with a QTimer or during the paintEvent() when you want to paint it on the screen.

                    Q Offline
                    Q Offline
                    Quiccz
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher I mean if i run this

                    app.exec();
                    
                    //this won't run until app.quit()
                    while(1)
                    {
                        //renderloop
                    }
                    
                    

                    Or

                    while(1)
                    {
                       //renderloop
                    }
                    
                    //exec won't run until render loop stop.
                    app.exec()
                    
                    

                    Or use timer

                    void loopfun
                    {
                        while(1)
                       {
                          //render
                       }
                    }
                    QSingalTimer ( 10, loopfun);
                    
                    app.exec();
                    

                    eventloop will be block, because my loopfun never end.

                    If need let them run both, thread must be used.

                    1 Reply Last reply
                    0
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Again: you don't need such a loop. As @Christian-Ehrlicher already suggested: either use a QTimer to trigger rendering or render in paintEvent()...

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

                      1 Reply Last reply
                      1

                      • Login

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