关闭

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

标签: QtQt按钮按钮美化
1637人阅读 评论(0) 收藏 举报
分类:

一,先上最终效果图

                                                                   

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    心得体会

     《程序员的心理疾病》
      1、概念不清
      2、大师崇拜
      3、各种乱七八糟的概念满天飞
      4、故步自封
      5、邪说盛行,歪道猖獗

     PS:个人随笔之文~如有差错,不吝赐教

    个人资料
    • 访问:58077次
    • 积分:1310
    • 等级:
    • 排名:千里之外
    • 原创:68篇
    • 转载:0篇
    • 译文:0篇
    • 评论:134条
    博客专栏
    最新评论