Qt实现图片渐进渐出效果

http://blog.sina.com.cn/s/blog_75deeddd0100tu2s.html


图片渐进渐出效果
.h文件
#ifndef CHANGEPICTURE_H
#define CHANGEPICTURE_H
#include <QWidget>
#include <QtGui>
#include <QGraphicsEffect>
#include <QGraphicsView>
#include <QTimer>
class ChangePicture : public QGraphicsView
{
Q_OBJECT
public:
    ChangePicture(QStringList piclist,
                  QSize picSize,
                  QPoint picPoint,
                  int speed,
                  QWidget *parent = 0);
private:
    void setupScene();
    void createItems();
private slots:
    void setupEffect();
private:
    QStringList m_piclist;
    QSize m_picSize;
    QPoint m_picPoint;
    QTimer m_speed;
    qreal m_angle;
    QGraphicsScene m_scene;
    QGraphicsItem *m_dissolving;
    QTimer *m_speedTimer;
    QPixmap m_pixmap;
    int m_timerCounter;
    int m_picCount;
    int m_int_speed;
};
#endif // CHANGEPICTURE_H


.cpp文件
#include "changepicture.h"

ChangePicture::ChangePicture(QStringList piclist, QSize picSize, QPoint picPoint, int speed ,QWidget *parent) :
        QGraphicsView(parent),m_piclist(piclist),m_picSize(picSize),m_picPoint(picPoint),m_int_speed(speed),m_angle(0.0)
{
    this->setupScene();
    this->setupEffect();
    this->m_timerCounter = 0;
    this->m_picCount = 1;
    this->m_speedTimer = new QTimer(this);
    connect(this->m_speedTimer,SIGNAL(timeout()),this,SLOT(setupEffect()));
    this->m_speedTimer->start(m_int_speed);
}
void ChangePicture::setupScene()
{
    this->setScene(&this->m_scene);
    this->setRenderHint(QPainter::Antialiasing,true);
    this->setFrameStyle(QFrame::NoFrame);
    this->m_scene.setSceneRect(0,0,this->m_picSize.width(),this->m_picSize.height());
    this->createItems();
}
void ChangePicture::createItems()
{
    this->m_pixmap = QPixmap(m_piclist.at(0)).scaled(m_picSize);
    this->m_dissolving = this->m_scene.addPixmap(this->m_pixmap);
    this->m_dissolving->setPos(m_picPoint);
}
void ChangePicture::setupEffect()
{
    QGraphicsOpacityEffect *m_dissolvingEffect = new QGraphicsOpacityEffect(this);
    if(this->m_timerCounter != 100)
    {
        m_dissolvingEffect->setOpacity(this->m_timerCounter * 0.02);
        this->m_timerCounter++;
    }else
    {
        this->m_scene.clear();
        m_dissolvingEffect->setOpacity(m_angle);
        this->m_pixmap = QPixmap(m_piclist.at(m_picCount)).scaled(m_picSize);
        this->m_dissolving = this->m_scene.addPixmap(this->m_pixmap);
        this->m_dissolving->setPos(m_picPoint);
        this->m_timerCounter = 0;
        if(this->m_picCount >= this->m_piclist.size() - 1)
        {
            this->m_picCount = 0;
        }else
        {
            this->m_picCount++;
        }
    }
    this->m_dissolving->setGraphicsEffect(m_dissolvingEffect);
}


阅读更多
个人分类: Qt Gui
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭