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. Call PaintEvent in for loop
Forum Updated to NodeBB v4.3 + New Features

Call PaintEvent in for loop

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 1.9k Views 2 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.
  • T Offline
    T Offline
    Tony123
    wrote on last edited by Tony123
    #1

    Hi guys,

    I want to make a scroll test performance. It have to work like that you scroll down a document by 1px then you call paintEvent and this have to happened 1000 times. So in for loop I write:

    for(int i=0;i<1000;++i){
        document->m_preview->Scroll(0,1); // perform 1pixel scroll
    
    }
    

    Problem is that the paintEvent is called only on the last scroll call. How can I force to repaint after all calling of scroll function?

    update(); repaint() doesn't seems to work!

    THX

    JonBJ jsulmJ 2 Replies Last reply
    0
    • T Tony123

      Hi guys,

      I want to make a scroll test performance. It have to work like that you scroll down a document by 1px then you call paintEvent and this have to happened 1000 times. So in for loop I write:

      for(int i=0;i<1000;++i){
          document->m_preview->Scroll(0,1); // perform 1pixel scroll
      
      }
      

      Problem is that the paintEvent is called only on the last scroll call. How can I force to repaint after all calling of scroll function?

      update(); repaint() doesn't seems to work!

      THX

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Tony123
      What Qt class is your document->m_preview?

      1 Reply Last reply
      0
      • T Tony123

        Hi guys,

        I want to make a scroll test performance. It have to work like that you scroll down a document by 1px then you call paintEvent and this have to happened 1000 times. So in for loop I write:

        for(int i=0;i<1000;++i){
            document->m_preview->Scroll(0,1); // perform 1pixel scroll
        
        }
        

        Problem is that the paintEvent is called only on the last scroll call. How can I force to repaint after all calling of scroll function?

        update(); repaint() doesn't seems to work!

        THX

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

        @Tony123 Well, you need to learn one thing: loops block Qt event loop (applies to all event driven applications) as long as they are executed. So, what you see is what you should expect. You should use a timer (QTimer) instead of a blocking loop.

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

        1 Reply Last reply
        4
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          Try calling QCoreApplication::processEvents() after each 1px scroll

          jsulmJ 1 Reply Last reply
          1
          • A Asperamanca

            Try calling QCoreApplication::processEvents() after each 1px scroll

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

            @Asperamanca Please not! This is bad design. OK, for testing I would agree :-)

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

            A 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Asperamanca Please not! This is bad design. OK, for testing I would agree :-)

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              @jsulm
              OP states that this is just for a test. Of course such constructs should be avoided in production code.

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                hehe
                If you are feeling guilty ;) , you can always show how to do the
                same thing with QTimer and a lambda.
                But for pure testing, i agree. no harm using QCoreApplication::processEvents()

                A 1 Reply Last reply
                1
                • Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

                  If your goal is to measure how many time it takes to paint a widget 1000 times, so QWidget::repaint() is the right choice, otherwise, I would recommend to use QWidget::update() instead of QWidget::repaint(). In this case, using loop and QCoreApplication::processEvents(), I think it will be the same. But with another design, this will let the event-loop repaint the widget only when it's necessary.

                  1 Reply Last reply
                  1
                  • mrjjM mrjj

                    hehe
                    If you are feeling guilty ;) , you can always show how to do the
                    same thing with QTimer and a lambda.
                    But for pure testing, i agree. no harm using QCoreApplication::processEvents()

                    A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #9

                    @mrjj said in Call PaintEvent in for loop:

                    hehe
                    If you are feeling guilty , you can always show how to do the
                    same thing with QTimer and a lambda.
                    But for pure testing, i agree. no harm using QCoreApplication::processEvents()

                    Not feeling guilty, because for a performance test, combining QTimer and a lambda would make it much harder to measure the actual time spent on repainting after scroll, assuming I understood the OP's intentions here correctly.

                    QElapsedTimer measureTimer;
                    measureTimer.start();
                    for(int i=0;i<1000;++i){
                        document->m_preview->Scroll(0,1); // perform 1pixel scroll
                        QCoreApplication::processEvents();
                    }
                    qDebug() << measureTimer.elapsed();
                    

                    and you are done, while still being very close to what the production code would do.

                    T 1 Reply Last reply
                    1
                    • A Asperamanca

                      @mrjj said in Call PaintEvent in for loop:

                      hehe
                      If you are feeling guilty , you can always show how to do the
                      same thing with QTimer and a lambda.
                      But for pure testing, i agree. no harm using QCoreApplication::processEvents()

                      Not feeling guilty, because for a performance test, combining QTimer and a lambda would make it much harder to measure the actual time spent on repainting after scroll, assuming I understood the OP's intentions here correctly.

                      QElapsedTimer measureTimer;
                      measureTimer.start();
                      for(int i=0;i<1000;++i){
                          document->m_preview->Scroll(0,1); // perform 1pixel scroll
                          QCoreApplication::processEvents();
                      }
                      qDebug() << measureTimer.elapsed();
                      

                      and you are done, while still being very close to what the production code would do.

                      T Offline
                      T Offline
                      Tony123
                      wrote on last edited by
                      #10

                      @Asperamanca thank you ..yes exactly this is what I did this helps for purpose of testing of course ty

                      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