日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Qt實現簡易毛玻璃效果的示例代碼_C 語言

作者:la_vie_est_belle ? 更新時間: 2022-08-01 編程語言

現有功能

1.用模糊功能實現簡易的毛玻璃效果。

2.鼠標移動無邊框窗口。

運行結果

源碼

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

原文鏈接:https://blog.csdn.net/La_vie_est_belle/article/details/125102683

欄目分類
最近更新