Qt實(shí)現(xiàn)簡易毛玻璃效果的示例代碼
現(xiàn)有功能
1.用模糊功能實(shí)現(xiàn)簡易的毛玻璃效果。
2.鼠標(biāo)移動無邊框窗口。
運(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è)變量用來移動窗口 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(); }
到此這篇關(guān)于Qt實(shí)現(xiàn)簡易毛玻璃效果的示例代碼的文章就介紹到這了,更多相關(guān)Qt毛玻璃效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++根據(jù)傳入的函數(shù)指針來解析需要的參數(shù)(推薦)
C++可以根據(jù)傳入的函數(shù)指針,獲取自己需要的參數(shù)類型,然后根據(jù)參數(shù)源中獲取需要的參數(shù),具體實(shí)現(xiàn)方式大家參考下本文2018-05-05OpenGL實(shí)現(xiàn)不規(guī)則區(qū)域填充算法
這篇文章主要為大家詳細(xì)介紹了OpenGL實(shí)現(xiàn)不規(guī)則區(qū)域填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02詳解C++語言中的加法運(yùn)算符與賦值運(yùn)算符的用法
這篇文章主要介紹了C++語言中的加法運(yùn)算符與賦值運(yùn)算符的用法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01C語言實(shí)現(xiàn)銷售管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)銷售管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++實(shí)現(xiàn)職工工資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單的職工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03