• Qt 图片轮播


    最近研究了一下图片轮播,主要是用到了QPropertyAnimation这个类,具体代码示例如下:

    main.cpp

    #include <QApplication>
    #include "widget.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        Widget view;
        view.show();
    
        return a.exec();
    }
    

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QPropertyAnimation>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
    
        ~Widget();
    
    protected:
        const QString popImage();
    
        void takeAnimation();
    
        void timerEvent( QTimerEvent *event );
    
    private:
        void onAnimation0Finished();
    
        void onAnimation1Finished();
    
    signals:
        void TakeAnimation();
    
    private:
        QVector<QString>    aryImage;
    
        QWidget             *widget0;
    
        QWidget             *widget1;
    
        QPropertyAnimation  *animation0;
    
        QPropertyAnimation  *animation1;
    
        Ui::Widget          *ui;
    };
    #endif // WIDGET_H
    

    widget.cpp

    #include <QPropertyAnimation>
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Widget)
    {
        ui->setupUi(this);
        setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
    
        // image
        aryImage << "c:/1.jpg"
                 << "c:/2.jpg"
                 << "c:/3.jpg";
    
        // widget
        widget0 = new QWidget( this );
        widget0->setFixedSize( 375, 200 );
    
        widget1 = new QWidget( this );
        widget1->setFixedSize( 375, 200 );
    
        // animation
        animation0 = new QPropertyAnimation();
        animation1 = new QPropertyAnimation();
    
        takeAnimation();
    
        startTimer( 5000 );
    }
    
    Widget::~Widget()
    {
        delete animation0;
        delete animation1;
        delete ui;
    }
    
    const QString Widget::popImage()
    {
        auto img = aryImage.first();
        aryImage.pop_front();
        aryImage.push_back( img );
        return img;
    }
    
    void Widget::takeAnimation()
    {
        if ( animation0->targetObject() == nullptr ) {
            widget0->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
            animation0->setTargetObject( widget0 );
        } else if ( animation0->targetObject() == widget1 ) {
            animation0->setTargetObject( widget0 );
        } else if ( animation0->targetObject() == widget0 ) {
            animation0->setTargetObject( widget1 );
        }
    
        animation0->setPropertyName( "geometry" );
        animation0->setDuration( 1500 );
        animation0->setStartValue( QRect( 0, 0, 375, 200 ) );
        animation0->setEndValue( QRect( -375, 0, 0, 200 ) );
        animation0->start();
    
        if ( animation1->targetObject() == nullptr ) {
            widget1->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
            animation1->setTargetObject( widget1 );
        } else if ( animation1->targetObject() == widget0 ) {
            widget1->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
            animation1->setTargetObject( widget1 );
        } else if ( animation1->targetObject() == widget1 ) {
            widget0->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
            animation1->setTargetObject( widget0 );
        }
    
        animation1->setPropertyName( "geometry" );
        animation1->setDuration( 1500 );
        animation1->setStartValue( QRect( 375, 0, 750, 200 ) );
        animation1->setEndValue( QRect( 0, 0, 375, 200 ) );
        animation1->start();
    }
    
    void Widget::timerEvent( QTimerEvent *event )
    {
        takeAnimation();
    }
    
  • 相关阅读:
    es6-箭头函数
    高度自适应的div
    移动端css水平垂直居中
    跨域2-常用的跨域方式
    跨域1-CORS跨域
    前端路由分类
    K8S系列-2.常用命令
    Git多分支管理
    微服务-01初识
    VirtualBox虚拟Centos磁盘文件扩容
  • 原文地址:https://www.cnblogs.com/isky0824/p/15592938.html
Copyright © 2020-2023  润新知