QPropertyAnimation 学习笔记7

QPropertyAnimation  用于产生动画效果。

 QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry");
  animation->setDuration(10000);
  animation->setStartValue(QRect(0, 0, 100, 30));
  animation->setEndValue(QRect(250, 250, 100, 30));

  animation->start();
这是文档中给出的例子,动画效果为,将窗口从0,0 位置缓慢移动到250,QPropertyAnimation 用来对Qt属性进行插值,Qt现在支持的QVariant 类型有QRect,QRectF,QLine,QLineF,QPoint,QColor,int ,double,float等。

这里为一个widget对象的geometry属性创建动画。

setDuration 设置动画时间,ms

setStartValue 设置开始属性

setEndValue 设置结束属性

start开始动画。

除了设置开始属性和结束属性外,还可以调用

void QVariantAnimation::setKeyValueAt(qreal step, const QVariant &value)

在动画中间设置属性值。取值范围为0.0-1.0,0开始,1结束。

其他使用参考文档。


这里将以一个例子说明如何在实战中使用:

这是360界面中的三个按钮,当鼠标进入或离开时,会有动画效果产生,博客中似乎不能上传动画,所以只能提供一张截图了。



代码很简单,也很容易懂,就不多说了。

class mainButton : public QPushButton//用于主的图片
{
    Q_OBJECT
public:
    mainButton(QString pixnormal,QString pixenter,QString pixleave,QWidget*parent);
    ~mainButton();
protected:
    void enterEvent(QEvent*);
    void leaveEvent(QEvent*);
    void paintEvent(QPaintEvent*event);

    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();}
};


mainButton::mainButton(QString strpixnormal,QString strpixenter,QString strpixleave,QWidget*parent):QPushButton(parent)
{
    QPixmap pixnormal(strpixnormal);
    QPixmap pixenter(strpixenter);
    QPixmap pixleave(strpixleave);

    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)));
}
mainButton::~mainButton()
{
    delete m_leaveanimation;
    delete m_enteranimation;
}
void mainButton::enterEvent(QEvent *)
{
    m_enter=true;
    m_leave=false;
    m_enteranimation->start();
}
void mainButton::leaveEvent(QEvent *)
{
    m_enter=false;
    m_leave=true;
    m_leaveanimation->start();
}
void mainButton::paintEvent(QPaintEvent *event)
{
    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));
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值