[Qt] Mask 蒙版

[Qt] Mask 蒙版

Mask能够覆盖在其他的widget上面,实现一些动态图片的加载效果。下面给出代码。

mask.h
#ifndef MASK_HJ
#define MASK_HJ

#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QLabel>

class CMask : public QWidget
{
    Q_OBJECT
public:
    CMask(QWidget *parent = 0);
    void m_vSetMessage(QString);
    void m_vSetPic(QString strPath);
    void m_vSetGif(QString strPath);

private:
    QLabel *m_infoLabel;
    void paintEvent(QPaintEvent*);
};

#endif // MASK_HJ
mask.cpp
#include <QMovie>
#include <QHBoxLayout>

CMask::CMask(QWidget *parent) :
    QWidget(parent)
{
    this->setStyleSheet("background-color: rgba(0, 0, 0, 0);");

    m_infoLabel =new QLabel(this);
    m_infoLabel->setFont(QFont("黑体", 30));
    m_infoLabel->setStyleSheet("background-color : rgba(0, 0, 0, 0);"
        "color: #776e65;"
        "margin: 0 auto");
    m_infoLabel->setAlignment(Qt::AlignCenter);

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(m_infoLabel);
    this->setLayout(mainLayout);

    this->hide();
}

void CMask::m_vSetMessage(QString s)
{
    m_infoLabel->setText(s);
    this->show();
}

void CMask::m_vSetPic(QString strPath)
{
    m_infoLabel->setPixmap(QPixmap(strPath));
    this->show();
}
void CMask::m_vSetGif(QString strPath)
{
    QMovie *movie = new QMovie(strPath);
    m_infoLabel->setMovie(movie);
    movie->start();
    this->show();
}

void CMask::paintEvent(QPaintEvent *){
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
调用方式
CMask *m_poMask = new CMask(this);
m_poMask->setGeometry(0, 0, 900, 480);
m_poMask->m_vSetGif(":/image/loading.gif");
m_poMask->show();

转载于:https://www.cnblogs.com/mdgsf/p/4841291.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值