Qt / PyQt 简易毛玻璃效果

Posted la_vie_est_belle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt / PyQt 简易毛玻璃效果相关的知识,希望对你有一定的参考价值。

目录

现有功能

运行结果

Qt源码

PyQt源码

github地址


现有功能

  1. 用模糊功能实现简易的毛玻璃效果。
  2. 鼠标移动无边框窗口。

运行结果

 

Qt源码

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();

PyQt源码

frosted_glass_label.py

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QApplication, QLabel, QGraphicsBlurEffect


class FrostedGlassLabel(QLabel):
    def __init__(self):
        super(FrostedGlassLabel, self).__init__()
        self.resize(300, 100)
        self.setWindowFlags(Qt.FramelessWindowHint)     # 去除边框

        # 这两个变量用来移动窗口
        self.start_x = None
        self.start_y = None

        self.set_background_color()
        self.blur()

    def set_background_color(self):
        """设置窗口背景颜色"""
        palette = QPalette()
        palette.setColor(QPalette.Background, QColor(245, 245, 245, 250))
        self.setPalette(palette)
        self.setAutoFillBackground(True)

    def blur(self):
        """模糊"""
        blur = QGraphicsBlurEffect()
        blur.setBlurRadius(30)
        blur.setBlurHints(QGraphicsBlurEffect.QualityHint)
        self.setGraphicsEffect(blur)

    def mousePressEvent(self, event):
        super(FrostedGlassLabel, self).mousePressEvent(event)
        self.start_x = event.x()
        self.start_y = event.y()

    def mouseMoveEvent(self, event):
        super(FrostedGlassLabel, self).mouseMoveEvent(event)
        dis_x = event.x() - self.start_x
        dis_y = event.y() - self.start_y
        self.move(self.x() + dis_x, self.y() + dis_y)


if __name__ == '__main__':
    app = QApplication([])
    fg_label = FrostedGlassLabel()
    fg_label.show()
    sys.exit(app.exec())

github地址

GitHub - la-vie-est-belle/Qt-PyQt

以上是关于Qt / PyQt 简易毛玻璃效果的主要内容,如果未能解决你的问题,请参考以下文章

Qt / PyQt 简易毛玻璃效果

PyQt5-Qt Designer简易的数字键盘输入

python3用pyqt5开发简易浏览器

让Qt支持Win7的Aero和毛玻璃效果

pyqt5实现一个简易音乐播放器(升级到v2版本)

QT绘制简易表盘