Qt实战 loading等待窗口实现

需求

  1. 执行耗时任务时,显示等待窗口,而不是盲等
  2. 耗时任务执行完毕后,等待窗口关闭
  3. 等待窗口只附着在父窗口,不影响其他窗口的操作

效果

 代码实现

 Loading.h

#pragma once

#include <QWidget>
#include <QVector>
#include <QImage>

class QTimer;
class Loading : public QWidget
{
    Q_OBJECT
public:
    explicit Loading(QWidget *parent, int timeout=30000);

protected:
    void paintEvent(QPaintEvent* e);
    void showEvent(QShowEvent* e);

private:
    QTimer* m_timer;
    int m_seq;
    bool firstShow;
    QVector<QImage> m_images;
};

Loading.cpp

#include "Loading.h"
#include <QPainter>
#include <QMessageBox>
#include <QTimer>

Loading::Loading(QWidget *parent, int timeout)
    : QWidget{parent},
      m_seq(0),
      firstShow(true)
{
    setWindowFlags(Qt::FramelessWindowHint);//无边框
    setAttribute(Qt::WA_TranslucentBackground);//背景透明

    this->resize(parent->geometry().size());
    this->move(0, 0);

    for (int i = 0; i < 8; ++i)
    {
        QString imgUrl = QString(":/resource/common_loading%1.png").arg(i);
        m_images << QImage(imgUrl);
    }

    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, [&]{
        this->update();
    });

    QTimer::singleShot(timeout, this, [this]{
        this->close();
        QMessageBox::warning(this, "warning", "waiting timeout!", QMessageBox::Ok);
    });
}

void Loading::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);

    painter.setBrush( QBrush(QColor(100, 100, 100, 100)));
    painter.drawRect(geometry());
    QRect rect((width() - 50)/2, (height()-50)/2, 50, 50);
    painter.drawImage(rect, m_images[m_seq++%8]);
}

void Loading::showEvent(QShowEvent *e)
{
    if (firstShow){
        m_timer->start(150);
        firstShow = false;
    }
}

调用代码

void MainWindow::on_pushButton_clicked()
{
    Loading l(this);
    l.show();

    EventLoop el;
    el.Exec([&]{
       std::this_thread::sleep_for(std::chrono::milliseconds(50000));
    });
}

资源文件

(1条消息) Loading等待窗口png资源文件-Javascript文档类资源-CSDN文库

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值