简单的利用qtimeLine实现,直接上代码
AnimationSigner::AnimationSigner(QWidget* parent) :
QWidget(parent),
m_frameIndex(0),
m_color(QColor(Qt::red))
{
InitWidget();
}
AnimationSigner::~AnimationSigner()
{
}
void AnimationSigner::SetColor(const QColor& color)
{
m_color = color;
}
bool AnimationSigner::ExecuteOne()
{
m_timeLine->start();
return true;
}
void AnimationSigner::ExecutedOne()
{
ExecuteOne();
}
void AnimationSigner::InitWidget()
{
setAttribute(Qt::WA_StyledBackground);
m_timeLine = new QTimeLine(500, this);//500ms内
m_timeLine->setFrameRange(0, 10);//触发0~10帧
m_timeLine->setLoopCount(2);//循环两次结束
connect(m_timeLine, &QTimeLine::frameChanged, this, [=](int index)
{
m_frameIndex = index;
update();
});
connect(m_timeLine, &QTimeLine::finished, this, [=]()
{
update();
delete this;
});
}
void AnimationSigner::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true); //消锯齿
int w = width();
int h = height();
int x = w / 2;
int y = h / 2;
//QColor color(Qt::red);
//内圆
int radiusIn = w / 4;
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(m_color, Qt::SolidPattern));
painter.drawEllipse(QPoint(x, y), radiusIn, radiusIn);
//外圆-持续动态
int radiusOut = radiusIn + ((float)m_frameIndex / 10.0) * radiusIn;
m_color.setAlphaF(0.7 * ((float)(10 - m_frameIndex) / 10.0));
painter.setBrush(QBrush(m_color, Qt::SolidPattern));
painter.drawEllipse(QPoint(x, y), radiusOut, radiusOut);
}
void AnimationSigner::mousePressEvent(QMouseEvent* event)
{
__super::mousePressEvent(event);
}
调用:
void MainWindow::CreateOneAnimation(const QPoint& point)
{
AnimationSigner* animation = new AnimationSigner(this);
int width = 40;
int height = 40;
animation->resize(width, height);
int posX, posY;
posX = point.x() - width / 2;
posY = point.y() - height / 2;
animation->move(posX, posY);
animation->raise();
animation->show();
animation->ExecutedOne();
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
CreateOneAnimation(event->pos());
}