Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在Qt中高效地循环图像?_C++_Qt - Fatal编程技术网

C++ 如何在Qt中高效地循环图像?

C++ 如何在Qt中高效地循环图像?,c++,qt,C++,Qt,我正在尝试制作一个像吃角子老虎机一样需要图像循环的应用程序。我有图像的顺序,他们需要循环,然后当一个按钮被按下时,他们需要停止在某个位置。 我知道我可以在指定的时间间隔使用QPixmap和重画,尽管我确信有一种更有效的方法。我想做的是以恒定的速度无限循环图像,一旦按下按钮,我将计算在哪个图像停止,开始减慢动画,并在x秒内在预定义的索引处停止。 我认为这里可以使用Qt动画框架。我只是不知道如何使无限循环。 提前感谢。我编写的代码的一个非常简化的版本: 它是一个小部件,可以显示动画文本和您想要的内容

我正在尝试制作一个像吃角子老虎机一样需要图像循环的应用程序。我有图像的顺序,他们需要循环,然后当一个按钮被按下时,他们需要停止在某个位置。 我知道我可以在指定的时间间隔使用QPixmap和重画,尽管我确信有一种更有效的方法。我想做的是以恒定的速度无限循环图像,一旦按下按钮,我将计算在哪个图像停止,开始减慢动画,并在x秒内在预定义的索引处停止。 我认为这里可以使用Qt动画框架。我只是不知道如何使无限循环。
提前感谢。

我编写的代码的一个非常简化的版本:

它是一个小部件,可以显示动画文本和您想要的内容

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();
    }
};

最困难的事情是计算最大偏移量…

我做的与此类似。在我的例子中,计算最大偏移量并不难,因为我显示的图像大小相同,间距相等。非常感谢。
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();
}