现有功能
1.用模糊功能实现简易的毛玻璃效果。
2.鼠标移动无边框窗口。
运行结果
源码
frosted_glass_label.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#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
1
2
3
4
5
6
7
8
9
10
11
|
#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毛玻璃效果内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/La_vie_est_belle/article/details/125102683