[Qt] Mask 蒙版

[Qt] Mask 蒙版

Mask可以覆蓋在其餘的widget上面,實現一些動態圖片的加載效果。下面給出代碼。ide

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();
相關文章
相關標籤/搜索