Qt實現(xiàn)簡易毛玻璃效果的示例代碼
更新時間:2022年06月07日 14:15:10 作者:la_vie_est_belle
這篇文章主要介紹了Qt如何利用模糊功能實現(xiàn)簡易的毛玻璃效果,并且鼠標(biāo)可以移動無邊框窗口,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
現(xiàn)有功能
1.用模糊功能實現(xiàn)簡易的毛玻璃效果。
2.鼠標(biāo)移動無邊框窗口。
運行結(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; // 這兩個變量用來移動窗口
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實現(xiàn)簡易毛玻璃效果的示例代碼的文章就介紹到這了,更多相關(guān)Qt毛玻璃效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++根據(jù)傳入的函數(shù)指針來解析需要的參數(shù)(推薦)
C++可以根據(jù)傳入的函數(shù)指針,獲取自己需要的參數(shù)類型,然后根據(jù)參數(shù)源中獲取需要的參數(shù),具體實現(xiàn)方式大家參考下本文2018-05-05
OpenGL實現(xiàn)不規(guī)則區(qū)域填充算法
這篇文章主要為大家詳細(xì)介紹了OpenGL實現(xiàn)不規(guī)則區(qū)域填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
C語言實現(xiàn)銷售管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)銷售管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

