Qt工作笔记-图像模糊处理,以及动画旋转

gif图很大,不好上传,就上传普通图片:

运行截图如下:

 

源码如下:

blureffect.h

#ifndef BLUREFFECT_H
#define BLUREFFECT_H

#include <QGraphicsBlurEffect>

QT_BEGIN_NAMESPACE
class QGraphicsItem;
QT_END_NAMESPACE

class BlurEffect : public QGraphicsBlurEffect
{
public:
    BlurEffect(QGraphicsItem *item);
    void setBaseLine(const qreal y);
    void draw(QPainter *painter)Q_DECL_OVERRIDE;

private:
    void adjustForItem();
    qreal m_baseLine;
    QGraphicsItem *m_item;

};

#endif // BLUREFFECT_H

mygraphicsview.h

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QGraphicsView>

class MyGraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    explicit MyGraphicsView(QWidget *parent = 0);

protected:
    void keyPressEvent(QKeyEvent *event)Q_DECL_OVERRIDE;

signals:
    void sendDelta(int delta);
};

#endif // MYGRAPHICSVIEW_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QGraphicsScene;
class QGraphicsItem;
class QPropertyAnimation;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(qreal index READ index WRITE setIndex)

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    qreal index()const;
    void setIndex(const qreal index);

protected slots:
    void animationRun(int delta);

private:
    Ui::Widget *ui;

    QGraphicsScene *m_scene;
    QList<QGraphicsItem*> m_icons;
    QPropertyAnimation *m_animation;
    qreal m_index;
};

#endif // WIDGET_H

blureffect.cpp

#include "blureffect.h"
#include <QGraphicsItem>
#include <QDebug>

BlurEffect::BlurEffect(QGraphicsItem *item)
    :QGraphicsBlurEffect()
    ,m_baseLine(200),m_item(item)
{

}

void BlurEffect::setBaseLine(const qreal y)
{
    m_baseLine=y;
}

void BlurEffect::draw(QPainter *painter)
{
    adjustForItem();
    QGraphicsBlurEffect::draw(painter);
}

void BlurEffect::adjustForItem()
{
    qreal y=m_baseLine-m_item->pos().y();
    qreal radius=qBound(qreal(0.0),y/32,qreal(16.0));
    setBlurRadius(radius);
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

mygraphicsview.cpp

#include "mygraphicsview.h"
#include <QDebug>
#include <QKeyEvent>

MyGraphicsView::MyGraphicsView(QWidget *parent)
    : QGraphicsView(parent)
{

}

void MyGraphicsView::keyPressEvent(QKeyEvent *event)
{
    int delta=0;
    switch(event->key()){
    case Qt::Key_Left:
        delta=-1;
        break;
    case Qt::Key_Right:
        delta=1;
        break;
    default:
        break;
    }
    emit sendDelta(delta);
}

wiget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "blureffect.h"
#include <QGraphicsScene>
#include <QPropertyAnimation>
#include <QDebug>
#include <QThread>
#include <QKeyEvent>
#include <QPixmap>
#include <QPointF>
#include <QtMath>
#include <QGraphicsItem>
#include <QGraphicsPixmapItem>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->graphicsView->setStyleSheet("background: transparent;border:0px");
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setRenderHints(ui->graphicsView->renderHints()|QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowState(Qt::WindowMaximized);

    m_index=0.0;
    m_scene=new QGraphicsScene;
    ui->graphicsView->setScene(m_scene);

    QStringList fileList;
    for(int i=0;i<10;i++){
        fileList<<":/img/"+QString::number(i+1)+".png";
    }

    for(int i=0;i<fileList.count();i++){
        QPixmap pixmap(fileList[i]);
        QGraphicsPixmapItem *icon=m_scene->addPixmap(pixmap);
        icon->setZValue(1);
        icon->setGraphicsEffect(new BlurEffect(icon));
        m_icons<<icon;
    }

    setIndex(0);

    m_animation=new QPropertyAnimation(this,"index");
    m_animation->setDuration(400);
    m_animation->setEasingCurve(QEasingCurve::InOutSine);
    ui->graphicsView->setRenderHint(QPainter::Antialiasing,true);
    connect(ui->graphicsView,SIGNAL(sendDelta(int)),this,SLOT(animationRun(int)));
    thread()->setPriority(QThread::TimeCriticalPriority);
}

Widget::~Widget()
{
    delete ui;
}

qreal Widget::index() const
{
    return m_index;
}

void Widget::setIndex(const qreal index)
{
    m_index=index;

    qreal baseline=0;
    for(int i=0;i<m_icons.count();i++){
        QGraphicsItem *icon=m_icons[i];
        qreal a=((i+m_index)*2*M_PI)/m_icons.count();
        qreal xs=500*qSin(a);
        qreal ys=300*qCos(a);
        QPointF pos(xs,ys);
        pos=QTransform().rotate(-20).map(pos);
        pos-=QPointF(40,40);
        icon->setPos(pos);
        baseline=qMax(baseline,ys);
        static_cast<BlurEffect*>(icon->graphicsEffect())->setBaseLine(baseline);
    }
    m_scene->update();
}

void Widget::animationRun(int delta)
{
    if(m_animation->state()==QAbstractAnimation::Stopped&&delta){

        m_animation->setEndValue(m_index+delta);
        m_animation->start();
    }
}

 

展开阅读全文
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值