小乌龟的博客

这些年开发之路的点滴,开源让人俞发前进

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

一,先上最终效果图

                                                                   

 由于制不来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


阅读更多
版权声明:转载请声明出处,谢谢~ https://blog.csdn.net/what951006/article/details/51542497
文章标签: Qt Qt按钮 按钮美化
所属专栏: Qt/C++开发实例教程
想对作者说点什么? 我来说一句

QT5.3.0界面调整实例小demo

2014年07月24日 6KB 下载

Qt 自定义形状窗体 Demo

2012年10月29日 152KB 下载

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

不良信息举报

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

最多只允许输入30个字

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