Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently loop images in Qt? [closed]

Tags:

c++

qt

I am trying to make an app that needs looping of images like in a Slot machine. I have the images in the order that they need to loop, and later when a button is pressed they'll need to stop at some position. I know I can use QPixmap and redraw at a specified interval, although I am pretty sure there's a more efficient way of doing it. What I want to do is to loop the images infinitely with constant speed, and once the button is pressed I will calculate at which image to stop, start slowing down the animation and stop at the predefined index in x seconds. I think that the Qt Animation Framework can be used here. I am just not sure how to make the infinite loop. Thanks in advance.

like image 406
Amy Avatar asked Dec 15 '25 15:12

Amy


1 Answers

A very simplified version of a code that I wrote :

It is a widget that display animated texts and almost what you want.

class Labels : public QFrame {
    Q_OBJECT
    Q_PROPERTY( int offset READ offset WRITE setOffset )
public:
    /* The property used to animate the view */
    int off;
    QStringList texts;
    Label() : QFrame() {
        texts << "text 1" << "text 2" << "text 3" << "text 4";
        setFixedSize( 200, 200 );
    }
    void paintEvent(QPaintEvent *) {
        QPainter painter( this );
        int x = 20;
        int y = 20;
        foreach( QString str, texts ) {
            int y1 = y + off;
            /* Used to draw the texts as a loop */
            /* If texts is underneath the bottom, draw at the top */
            if ( y1 > height() ) { 
                y1 -= height();
            }
            painter.drawText( x, y1, str );
            y+= 50;
        }
    }

    int offset() {
        return off;
    }

    void setOffset( int o ) {
        off = o;
        update();
    }
};

The main :

int main( int argc, char **argv) {
    QApplication app(argc, argv, true);
    Labels l;
    l.show();

    /* Animated the view */
    QPropertyAnimation *animation = new QPropertyAnimation(&l,"offset");
    animation->setLoopCount( -1 ); /* infinite loop */
    animation->setDuration(2000);
    animation->setStartValue(0.0);
    animation->setEndValue(200.0);
    animation->start();
    return app.exec();
}

The hardest thing is to calculate the max offset...

like image 107
Dimitry Ernot Avatar answered Dec 17 '25 23:12

Dimitry Ernot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!