Qt mask layer, waiting for background processing animation effect

This is useful when a large amount of data needs to be processed in the background and the waiting interface is displayed in the foreground.

The content on the interface is completely blocked, and there is a translucent shielding layer behind it. You cannot click to operate the back interface. Users cannot cancel the waiting interface by themselves to prevent misoperation.
And when the size of the window changes, it automatically adapts to the interface change, does not block the window movement and enlargement and reduction, and is non-modal.
It is not the bad effect of window screenshots in C#, it is really masked, and the background is translucent.

Need to cooperate with multi-threaded use, the foreground displays the waiting interface, and the background thread processes the data. When the processing is completed, a message is sent through the signal slot to notify the foreground to cancel the waiting state.

Insert picture description hereSupplement:
A friend asked how the source code of this thing was made, and posted it below:
1. Create a new Qt widget with a class name of LoadingForm and a ui interface. Put a QLabel on the interface and adjust it to the size of the animation. , Using grid layout, centered display.
Insert picture description here
2. The code of LoadingForm is as follows:
header file LoadingForm.h:

#ifndef LOADINGFORM_H
#define LOADINGFORM_H

#include <QWidget>

namespace Ui {
    
    
class LoadingForm;
}

class LoadingForm : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit LoadingForm(QWidget *parent = nullptr);
    ~LoadingForm();

private:
    Ui::LoadingForm *ui;
};

#endif // LOADINGFORM_H

Implementation file LoadingForm.cpp:

#include "loadingform.h"
#include "ui_loadingform.h"

#include <QMovie>

LoadingForm::LoadingForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoadingForm)
{
    
    
    ui->setupUi(this);

    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);

    QMovie *movie;
    movie = new QMovie(":/icon/res/loading.gif");
    ui->label->setMovie(movie);
    movie->start();

    ui->label->setStyleSheet("background-color: transparent;");
}

LoadingForm::~LoadingForm()
{
    
    
    delete ui;
}

3. Add display code on MainWindow: In the
header file:

private:    
    QWidget* m_pMaskLayer = nullptr;
protected:
    virtual void resizeEvent(QResizeEvent *event) override;

Implementation file:

MainWindow6::MainWindow6(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow6)
{
    
    
    ui->setupUi(this);    
    m_pMaskLayer = new LoadingForm(this);  
    m_pMaskLayer->setFixedSize(this->size());//设置窗口大小
    m_pMaskLayer->setVisible(false);//初始状态下隐藏,待需要显示时使用
    this->stackUnder(m_pMaskLayer);//其中pWrapper为当前窗口的QWidget
}

void MainWindow6::resizeEvent(QResizeEvent *event)
{
    
    
    if(event){
    
    }//消除警告提示

    if(m_pMaskLayer != nullptr)
    {
    
    
        m_pMaskLayer->setAutoFillBackground(true); //这个很重要,否则不会显示遮罩层
        QPalette pal = m_pMaskLayer->palette();
        pal.setColor(QPalette::Background, QColor(0x00,0x00,0x00,0x20));
        m_pMaskLayer->setPalette(pal);
        m_pMaskLayer->setFixedSize(this->size());
    }
}
//显示
void MainWindow6::showLoadingForm()
{
    
    
    if(m_pMaskLayer != nullptr)
    {
    
    
        m_pMaskLayer->setVisible(true);
    }
}
//隐藏
void MainWindow6::hideLoadingForm()
{
    
    
    if(m_pMaskLayer != nullptr)
    {
    
    
        m_pMaskLayer->setVisible(false);
    }
}

With multithreading, you can directly showLoadingForm() and hideLoadingForm() when using it, or you can add a global signal slot yourself.
Note: Don't mix time-consuming operations in the UI thread, or it will get stuck and useless.

Guess you like

Origin blog.csdn.net/lixiaoxing2/article/details/109378419