Qt毛玻璃效果
la_vie_est_belle 人气:0现有功能
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(); }
加载全部内容