[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();
posted @ 2015-09-26 21:05  MDGSF  阅读(1200)  评论(0编辑  收藏  举报