QT可以实现一下简单的动画,比如 运动的时钟 闪烁的按钮。
动画的实现:
(1)创建一个定时器
(2)调用QWidget::update()通知界面重绘
实现一个按钮闪烁的例子:
circlewidget.h
#ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QTimer> class CircleWidget:public QFrame { Q_OBJECT public: CircleWidget(QWidget *parent); ~CircleWidget(); private: void paintEvent(QPaintEvent *event); void timerEvent(QTimerEvent *event); private: int m_timerId; int m_times; }; #endif // CIRCLAWIDGET_H
circlewidgit.cpp
# include"circlewidget.h" #include<QPainter> CircleWidget::CircleWidget(QWidget *parent):QFrame(parent) { m_timerId=startTimer(1000); m_times=0; } CircleWidget::~CircleWidget() { } void CircleWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); int width=this->width(); int height=this->height(); painter.setPen(Qt::NoPen); if(m_times==0) { QBrush brush(QColor(0xFF,0xFF,0xFF)); painter.setBrush(brush); m_times=1; } else { QBrush brush(QColor(0x88,0x00,0xFF)); painter.setBrush(brush); m_times=0; } m_times++; if(m_times==1) { m_times=0; } painter.drawEllipse(0,0,40,40); } void CircleWidget::timerEvent(QTimerEvent *event) { update(); }