Qt實(shí)現(xiàn)簡易毛玻璃效果的示例代碼
更新時(shí)間:2022年06月07日 14:15:10 作者:la_vie_est_belle
這篇文章主要介紹了Qt如何利用模糊功能實(shí)現(xiàn)簡易的毛玻璃效果,并且鼠標(biāo)可以移動(dòng)無邊框窗口,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
現(xiàn)有功能
1.用模糊功能實(shí)現(xiàn)簡易的毛玻璃效果。
2.鼠標(biāo)移動(dòng)無邊框窗口。
運(yùn)行結(jié)果

源碼
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(); // 設(shè)置窗口背景顏色
void blur(); // 模糊
private:
float startX; // 這兩個(gè)變量用來移動(dòng)窗口
float startY;
};
#endif // FROSTEDGLASSLABEL_Hfrosted_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();
}到此這篇關(guān)于Qt實(shí)現(xiàn)簡易毛玻璃效果的示例代碼的文章就介紹到這了,更多相關(guān)Qt毛玻璃效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)打地鼠游戲設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)打地鼠游戲設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
C++ Boost Serialization庫超詳細(xì)獎(jiǎng)金額
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱2022-12-12
C++ OpenCV實(shí)現(xiàn)白平衡之灰度世界算法
灰度世界算法是白平衡各種算法中最基本的一種。本文將利用C++和OpenCV實(shí)現(xiàn)白平衡中的灰度世界算法,文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05

