Qt 之等待提示框(QTimer)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011012932/article/details/51029355

简述

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

| 版权声明:一去、二三里,未经博主允许不得转载。

效果

这里写图片描述

资源

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

这里写图片描述

源码

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

更多参考

猜你喜欢

转载自blog.csdn.net/u011012932/article/details/51029355