QT实现控件倒影特效 2.0

QT实现控件倒影特效 2.0

之前写过一篇文章关于QT如何实现倒影特效的,现在完善了一下,将程序写成接口,方便调用,同时使倒影特效更加逼真

想要参考控件特效1.0的,传送门在这里:http://blog.csdn.net/fan_xingwang/article/details/78982571

话不多说,直接上代码

reflect文件(包括.cpp和.h文件)就是写好的接口,直接调用Reflect类中的getMirror()方法就可以得到想要的控件的倒影

reflect.h

#ifndef REFLECT_H
#define REFLECT_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QPixmap>

class Reflect : public QWidget
{
public:
    Reflect();
    void paintEvent(QPaintEvent*);
    void getMirror(QWidget*);

public:
    QPushButton *pBtn;
    QLabel* pLabel;
    QPixmap* pmirrorPixmap;
    QColor color;
};

#endif // REFLECT_H

reflect.cpp

#include "reflect.h"
#include <QVBoxLayout>
#include <QPainter>
#include <QDateTime>
#include <QDebug>

Reflect::Reflect()
{
    QPalette pal(this->palette());
    pal.setColor(QPalette::Background, Qt::black);
    QBrush brush = pal.background();
    color = brush.color();
    this->setAutoFillBackground(true);
    this->setPalette(pal);
}

/***************************************************************************
* Function:重写painEvent函数,实现倒影特效
* InPut :QPaintEvent *
* OutPut :倒影特效
* Return :void
* Other :
* Author : fanxingwang %{CurrentDate:2018.01.10}
***************************************************************************/
void Reflect::paintEvent(QPaintEvent *)
{  
    QPainter painter(this);
    painter.save();
    painter.scale(1,-1);
    painter.translate(0,-pmirrorPixmap->height());

    painter.setRenderHint(QPainter::Antialiasing, true);//反走样
    // 设置渐变色
    QLinearGradient linear(0,pmirrorPixmap->height(),0,0);
    linear.setColorAt(0.4,color);//将图片40%用背景色覆盖
    linear.setColorAt(1.0,Qt::transparent);

    painter.drawPixmap(0,0,*pmirrorPixmap);
    painter.restore();
    painter.fillRect(QRect(0,0,pmirrorPixmap->width(),pmirrorPixmap->height()),
                      QBrush(linear));
}

/***************************************************************************
* Function:将传进来的QWidget的内容、大小等信息存储进QPixmap
* InPut :QWidget *
* OutPut :QPixmap
* Return :void
* Other :
* Author : fanxingwang %{CurrentDate:2018.01.10}
***************************************************************************/
void Reflect::getMirror(QWidget *widget)
{
    pmirrorPixmap = new QPixmap;
    *pmirrorPixmap = QPixmap::grabWidget(widget,0,0,widget->width(),widget->height());
    this->resize(widget->width(),widget->height());
}

以下是mainwindow代码,我分别用了按钮和图片测试,要注意,getMirror(QWidget *widget)方法传进来的参数是QWidget,所以放图片时,需要传进去的参数是放置图片的label

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include "reflect.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);

    ~MainWindow();

private:
    QPushButton *pBtn;
    Reflect *pReflect;
    QLabel *pLabel;
    QImage pImage;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QVBoxLayout>
#include <QPixmap>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setMinimumSize(500,500);

    QPalette pal(this->palette());
    pal.setColor(QPalette::Background, Qt::black);
    this->setAutoFillBackground(true);
    this->setPalette(pal);

//    pBtn = new QPushButton(this);
//    pBtn->setText("This is a Button");
//    pBtn->setGeometry(0,0,300,100);
//    connect(pBtn,SIGNAL(clicked(bool)),this,SLOT(close()));

    pReflect = new Reflect();

    pLabel = new QLabel(this);
    pLabel->setGeometry(0,0,200,120);

    if(pImage.load(":/1.png"))
    {
        this->pLabel->setPixmap(QPixmap::fromImage(pImage));
    }

    pReflect->getMirror(pLabel);
    pReflect->show();

}

MainWindow::~MainWindow()
{

}

运行结果:
这里写图片描述

这里写图片描述

这里我为了使之显示的更加真实,所以我只显示60%内容,最终的结果就是生成了一个只含有倒影图片的widget,自己可自由放置。

想要参考控件特效1.0的,传送门在这里:http://blog.csdn.net/fan_xingwang/article/details/78982571

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值