Qt之等待提示框(QTimer)

时间:2023-03-08 15:48:16

简述

上节讲述了关于QPropertyAnimation实现等待提示框的显示,本节我们使用另外一种方案来实现-使用定时器QTimer,通过设置超时时间定时更新图标达到旋转效果。

效果

Qt之等待提示框(QTimer)

资源

需要几张不同阶段的图标进行切换,这里使用8张。

Qt之等待提示框(QTimer)

源码

QTimer通过setInterval设置100毫秒超时时间,每隔100毫秒后进行图标的更换,达到旋转效果。

MainWindow::MainWindow(QWidget *parent)
: CustomWindow(parent),
m_nIndex(1)
{
m_pLoadingLabel = new QLabel(this);
m_pTipLabel = new QLabel(this);
m_pTimer = new QTimer(this); m_pTipLabel->setText(QString::fromLocal8Bit("拼命加载中...")); // 设定超时时间100毫秒
m_pTimer->setInterval(100);
connect(m_pTimer, &QTimer::timeout, this, &MainWindow::updatePixmap); startAnimation();
}
// 启动定时器
void MainWindow::startAnimation()
{
m_pTimer->start();
} // 停止定时器
void MainWindow::stopAnimation()
{
m_pTimer->stop();
} // 更新图标
void MainWindow::updatePixmap()
{
// 若当前图标下标超过8表示到达末尾,重新计数。
m_nIndex++;
if (m_nIndex > 8)
m_nIndex = 1; QPixmap pixmap(QString(":/Images/loading%1").arg(m_nIndex));
m_pLoadingLabel->setPixmap(pixmap);
}

更多参考