1、结合定时器来实现
// 按钮slot
void MyAnimationWindow::StartAnimation() {
m_timerId = startTimer(300);
}
// 重写定时器事件
void MyAnimationWindow::timerEvent(QTimerEvent *event) {
if (event->timerId() == m_timerId) {
update();// 重绘
}
}
// 重写绘制事件
void MyAnimationWindow::paintEvent(QPaintEvent *event) {
QPainter painter(this);
if (m_drawTimes == 0) {
QPen pen(QColor(0, 0, 255));
QBrush brush(QColor(0, 0, 255));
painter.setPen(pen);
painter.setBrush(brush);
painter.drawEllipse(QPoint(80, 80), 40, 40);
m_drawTimes++;
}
else {
QPen pen(QColor(100, 100, 100));
QBrush brush(QColor(100,100, 100));
painter.setPen(pen);
painter.setBrush(brush);
painter.drawEllipse(QPoint(80, 80), 40, 40);
m_drawTimes++;
m_drawTimes = 0;
}
}
效果图
image.png
注意:
update() 函数只是通知界面重新绘制,会在事件循环中产生一个“绘制事件”paint event,等该事件前面的事件完成后才重新绘制。
网友评论