Qt/C++ 360安全卫士主界面动态按钮demo

来源:互联网 发布:竞彩缩水软件 编辑:程序博客网 时间:2024/05/19 00:14

一,先上最终效果图

                                                                   

 由于制不来gif就只能传个hover状态的了,所以看不出实际效果了

                                 

二,主要代码:

      1.头文件

 #include<QObject>

#include <QWidget>
#include<QPaintEvent>
#include<QEvent>
#include<QPushButton>
#include<qpropertyanimation.h>
#include<QDebug>
class my360button : public QPushButton
{
    Q_OBJECT
public:
    my360button(QPixmap pixnormal,QPixmap pixenter,QPixmap pixleave,QWidget*parent);
    ~my360button();
protected:
    void enterEvent(QEvent*);
    void leaveEvent(QEvent*);
    void paintEvent(QPaintEvent*);
    QPropertyAnimation*m_enteranimation;
    QPropertyAnimation*m_leaveanimation;
    QList<QPixmap> m_enterlist;
    QList<QPixmap> m_leavelist;
    QPixmap m_pixnormal;
    int m_enterIndex;
    int m_leaveIndex;
    bool m_enter;
    bool m_leave;
public slots:
    void entervaluechange(QVariant var){m_enterIndex=var.toInt();update();}
    void leavevaluechange(QVariant var){m_leaveIndex=var.toInt();update();qDebug()<<m_leaveIndex;}
};

 .cpp文件


#include"my360button.h"

#include<QPainter>
#include<QDebug>
my360button::my360button(QPixmap pixnormal,QPixmap pixenter,QPixmap pixleave,QWidget*parent):QPushButton(parent)
{
    setCursor(Qt::PointingHandCursor);
    m_leave=false;
    m_enter=true;
    m_leaveIndex=0;
    m_enterIndex=0;
    m_pixnormal=pixnormal;
    for(int i=0;i<10;i++)//进入
    {
        m_enterlist<<pixenter.copy(i*(pixenter.width()/10),0,pixenter.width()/10,pixenter.height());
    }
    for(int j=0;j<8;j++)//离开
    {
        m_leavelist<<pixleave.copy(j*(pixleave.width()/8),0,pixleave.width()/8,pixleave.height());
    }
    m_enteranimation=new QPropertyAnimation(this,"");
    m_enteranimation->setStartValue(0);
    m_enteranimation->setEndValue(9);
    m_enteranimation->setDuration(600);
    connect(m_enteranimation,SIGNAL(valueChanged(QVariant)),this,SLOT(entervaluechange(QVariant)));
    m_leaveanimation=new QPropertyAnimation(this,"");
    m_leaveanimation->setStartValue(0);
    m_leaveanimation->setEndValue(7);
    m_leaveanimation->setDuration(600);
    connect(m_leaveanimation,SIGNAL(valueChanged(QVariant)),this,SLOT(leavevaluechange(QVariant)));
}
my360button::~my360button()
{
    delete m_leaveanimation;
    delete m_enteranimation;
}
void my360button::enterEvent(QEvent *)
{
    m_enter=true;
    m_leave=false;
    m_enteranimation->start();
}
void my360button::leaveEvent(QEvent *)
{
    m_enter=false;
    m_leave=true;
    m_leaveanimation->start();
}
void my360button::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
   if(m_enter)
    painter.drawPixmap(rect(),m_enterlist.at(m_enterIndex));
   if(m_leave)
    painter.drawPixmap(rect(),m_leavelist.at(m_leaveIndex));
}

三,demo下载:http://download.csdn.net/detail/what951006/9535965


powered by 小乌龟在大乌龟背上

更多文章:http://blog.csdn.net/what951006


2 0
原创粉丝点击