Need a Timer which works exactly



  • Hello,
    I need a Timer which works really exactly. Has somebody an idea ?
    In my application there is the usecase to change an Image in a constant intervall with another one. For example every 100 ms.
    Okay. There is the QML Timer.

    But this Timer doesn't work independent from the running application and so the Timer never works exactly. If I do something more at the same time (scrolling a listview, moving the mouse) it gets really bad.


  • Moderators

    @Snoopy said in Need a Timer which works exactly:

    In my application there is the usecase to change an Image in a constant intervall with another one. For example every 100 ms.

    For this use-case, it is best not to use a timer to trigger image loading.

    Ideally, all your images should be combined into one file (e.g. an animated GIF file or a sprite file), and played with AnimatedImage (QML), AnimatedSprite (QML), or QMovie (widgets).

    Anyway:

    1. How big are your images?
    2. What is your code for changing the images?


  • Thanks for the info. I will check it.

    One image has 300 x 300 Pixels and format is actually PNG. Size of one image is 20 KB.
    In the worstcase in one run I've to show 120 images. For example every 100 ms the next one.

    Below the code how I do it at the moment.

        Timer {
            id:timer
               interval: 100;
               running: false;
               property int ix: 0
    
               onTriggered:
               {
                   if (ix == 120)
                       timer.stop();
                    else
                  {
                   ix++;
    
                   myControl.step = ix;
                   running = true;
                }
      }
    

    Item{
    id: myControl
    property int step: 0

    Image
    {
        id: image
        source: "images/Step" + myControl.step + ".png"
        visible: true
        cache: true
    }
    

    }


  • Moderators

    @Snoopy said in Need a Timer which works exactly:

    One image has 300 x 300 Pixels and format is actually PNG. Size of one image is 20 KB.
    In the worstcase in one run I've to show 120 images. For example every 100 ms the next one.

    You could merge the 120 images into one large PNG sprite grid (12 x 10 images = 3600 x 3000 pixels), and display it using an AnimatedSprite.

    Or, you could merge the images into 1 animated GIF or 1 animated MNG, and display it using AnimatedImage.

    Image
    {
        source: "images/Step" + myControl.step + ".png"
    }
    

    Your code starts loading each image when the timer triggers. So, this is the sequence:

    1. Wait for timer to trigger
    2. Load image from disk
    3. Display image
    4. Goto #1

    Step #2 is very slow. If you can find a way to load the images into memory before you start the timer, you might see better performance.



  • Okay thanks for the answer.

    But how can I set the interval in the AnimatedSprite ? I want to replace an image with the next one everytime after 100 ms.
    If I want to run the AnimatedSprite with another interval for example 250 ms or 500 ms. How can I do that ?



  • @Snoopy without having it tested:

    AnimatedImage has the properties:

    currentFrame : int
    frameCount : int
    paused : bool
    playing : bool

    so:

    paused: true
    currentFrame: myControl.step
    

  • Moderators

    In addition to @J-Hilk's properties, there's also frameRate or frameDuration: http://doc.qt.io/qt-5/qml-qtquick-animatedsprite.html#frameDuration-prop


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.