现有功能
1.用模糊功能实现简易的毛玻璃效果。
2.鼠标移动无边框窗口。
运行结果
源码
frosted_glass_label.h
#ifndef FROSTEDGLASSLABEL_H
#define FROSTEDGLASSLABEL_H
#include <QWidget>
#include <QLabel>
#include <QMouseEvent>
class FrostedGlassLabel : public QLabel
{
Q_OBJECT
public:
FrostedGlassLabel(QWidget *parent = nullptr);
~FrostedGlassLabel();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
void setBackgroundColor(); // 设置窗口背景颜色
void blur(); // 模糊
private:
float startX; // 这两个变量用来移动窗口
float startY;
};
#endif // FROSTEDGLASSLABEL_H
frosted_glass_label.cpp
#include "frosted_glass_label.h"
#include <Qt>
#include <QPalette>
#include <QColor>
#include <QGraphicsBlurEffect>
FrostedGlassLabel::FrostedGlassLabel(QWidget *parent)
: QLabel(parent)
{
this->resize(300, 100);
this->setWindowFlags(Qt::FramelessWindowHint);
this->setBackgroundColor();
this->blur();
}
FrostedGlassLabel::~FrostedGlassLabel()
{
}
void FrostedGlassLabel::setBackgroundColor() {
QPalette palette;
palette.setColor(QPalette::Background, QColor(245, 245, 245, 250));
this->setPalette(palette);
this->setAutoFillBackground(true);
}
void FrostedGlassLabel::blur() {
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
blur->setBlurRadius(30);
blur->setBlurHints(QGraphicsBlurEffect::QualityHint);
this->setGraphicsEffect(blur);
}
void FrostedGlassLabel::mousePressEvent(QMouseEvent *event) {
QLabel::mousePressEvent(event);
this->startX = event->x();
this->startY = event->y();
}
void FrostedGlassLabel::mouseMoveEvent(QMouseEvent *event) {
QLabel::mouseMoveEvent(event);
float disX = event->x() - this->startX;
float disY = event->y() - this->startY;
this->move(this->x()+disX, this->y()+disY);
}
main.cpp
#include "frosted_glass_label.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrostedGlassLabel w;
w.show();
return a.exec();
}
到此这篇关于Qt实现简易毛玻璃效果的示例代码的文章就介绍到这了,更多相关Qt毛玻璃效果内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!