Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Moved] Timer used to repaint (advance) delayed by event flood processing
Forum Updated to NodeBB v4.3 + New Features

[Moved] Timer used to repaint (advance) delayed by event flood processing

Scheduled Pinned Locked Moved Mobile and Embedded
46 Posts 7 Posters 26.0k 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.
  • D Offline
    D Offline
    dflorin
    wrote on last edited by
    #1

    I have a scene with a ball and a bat. The ball is advance using a timer (frequency about 30 / sec). The bat is moved using the mouse events (or touch events).
    The problem is that when I move the bat continuously, the ball is slowing down (stopping in between).

    I printed out some traces and the issue seems to be related to the fact that the timer is not triggered because of the mouse (touch) events flood. In the worst cases the timer is called once or twice a second.

    @
    const int TimerFreq = 1000/33;
    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
    timer.start(TimerFreq);@

    How can I rework this in order to get the ball moving smoothly, not depending on the mouse/touch events.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      You can move your ball work to another thread with its own event loop.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        milot.shala
        wrote on last edited by
        #3

        As Denis suggested "QThread":http://doc.qt.nokia.com/4.7/qthread.html is the best place to start.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          How many events are we talking about? Which device is it, that is generating so many events (and they're not somehow filtered)? Which platform?

          [quote]
          You can move your ball work to another thread with its own event loop.
          [/quote]

          Nonsense. QGraphics* classes are not reentrant, thus they're usable only from the main thread, whose event loop seems to be flooded by touch events.

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            peppe, who is saying about moving QGraphics* to another thread? We can move all calculations of new ball position to another thread and left only painting at main thread. So calculations will be made every 1/30 sec and will be painted asap.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #6

              They won't be painted because painting requires the MAIN EVENT LOOP to be running.

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DenisKormalev
                wrote on last edited by
                #7

                peppe, I think if all work about ball and about bat moving will be moved away from main thread (i.e. we will have only painting and catching events, all calculations will be done in background) then it will help.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dangelog
                  wrote on last edited by
                  #8

                  How can it help if the main thread's event loop is flooded by those mouse/touch events? Even supposing that you can pick the data from the main thread, do your math in 1 millionth of a second, and schedule an update() for your item, the QPaintEvent for the QGV will be sitting in the main thread's event queue and will actually be delivered after 1-2s (according to OP).

                  Software Engineer
                  KDAB (UK) Ltd., a KDAB Group company

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DenisKormalev
                    wrote on last edited by
                    #9

                    peppe, according to OP timer events are invoked once in 1-2 seconds.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      Main thread does all the GUI stuff.

                      "background" thread does all the ball calculations and sends a signal to the main thread which in turn does the graphics update.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dangelog
                        wrote on last edited by
                        #11

                        [quote author="Denis Kormalev" date="1293486729"]peppe, according to OP timer events are invoked once in 1-2 seconds.[/quote]

                        Instead of the desidered 30 updates per second.

                        [quote]“background” thread does all the ball calculations and sends a signal to the main thread which in turn does the graphics update.[/quote]

                        Again, HOW does the main thread update the view if the main event loop is flooded by input events?

                        Software Engineer
                        KDAB (UK) Ltd., a KDAB Group company

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DenisKormalev
                          wrote on last edited by
                          #12

                          Volker, exactly what I am saying :) Also I think not only ball, but bat calculations can be moved to background thread too.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            I did not say, it solves the problem. I did say, it is possible to do it this way without violating the gui-update-only-in-main-thread rule.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dflorin
                              wrote on last edited by
                              #14

                              [quote author="Volker" date="1293486907"]Main thread does all the GUI stuff.

                              "background" thread does all the ball calculations and sends a signal to the main thread which in turn does the graphics update.
                              [/quote]

                              Calculating the next ball position is just x=x+1. The problem is that the timer's timeout() is not triggered as expected (30 times a second), because the main thread is busy handling the mouse/touch events.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                DenisKormalev
                                wrote on last edited by
                                #15

                                peppe, at least OP can try it and maybe it will solve the problem.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  dangelog
                                  wrote on last edited by
                                  #16

                                  Wtf? Is this some kind of SERIOUS help or just "try this random thing"?

                                  Software Engineer
                                  KDAB (UK) Ltd., a KDAB Group company

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    DenisKormalev
                                    wrote on last edited by
                                    #17

                                    peppe, forum is "try this thing" at 85% of questions.
                                    dflorin, try to count how often paintEvent is invoked. If it invoked in good frequency then using background thread will help.

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      dangelog
                                      wrote on last edited by
                                      #18

                                      [quote]peppe, forum is “try this thing” at 85% of questions. [/quote]
                                      This dramatically makes a forum a low-quality forum.

                                      Still, after 10 posts, noone managed to explain me WHY your suggestion should work.

                                      Software Engineer
                                      KDAB (UK) Ltd., a KDAB Group company

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        DenisKormalev
                                        wrote on last edited by
                                        #19

                                        peppe, I don't know forum where you can ask any question you have stucked with and receive answer correctly and quickly. And this discussion is offtopic. If you have something to add about subj of topic then add.

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          dflorin
                                          wrote on last edited by
                                          #20

                                          Back to the subject. I made some measurements:

                                          When not moving the bat:

                                          • Bat::paint() called 30 times (how many times the bat is repainted as a result of setPos() called from the event() method)
                                          • Ball::advance() called 30 times (this is called by the timer and should be 30 calls instead) - this will trigger paint()

                                          NOTE: the Bat is also repainted because I use:
                                          view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
                                          This should not have any impact on my problem.

                                          When moving the bat as fast as I can using touch events:

                                          • Scene::event() called 47 times (this is how fast I move the bat by touching the screen)
                                          • Bat::paint() called 50 times (how many times the bat is repainted as a result of setPos() called from the event() method and as a result of paint() done for the ball)
                                          • Ball::advance() called 13 times (this is called by the timer and should be 30 calls instead) - this is my problem: the ball has slowed down at less than half speed.

                                          And one more thing: this is noticeable on a Symbian device. Running it in the simulator on Windows and using mouse events instead of touch events does not slow down the ball. Should the post be moved to Mobile and Embedded?

                                          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