Qt: How to create a pronounced glow effect for QLabel? (e.g. using QGraphicsDropShadowEffect)

I am trying to add a glow effect to QLabel so that it looks like a time display in the following figure:

enter image description here

I found out that you can "misuse" QGraphicsDropShadowEffect for this:

 QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect(); dse->setBlurRadius(10); dse->setOffset(0); dse->setColor(QColor(255, 255, 255)); ui.label->setGraphicsEffect(dse); 

However, the resulting effect is too weak, you can hardly see it:

enter image description here

Unfortunately, you cannot change the strength of the effect, only the radius of color and blur.

One idea would be to apply a few QGraphicsDropShadowEffect to the label to make it more visible due to overlapping. But calling ui.label->setGraphicsEffect(dse); always deletes any previous effects, i.e. I was unable to install multiple QGraphicsEffect for the same object.

Any ideas on how to create a clearly visible glow effect using Qt?

+5
source share
1 answer

Meanwhile, I applied my own graphic effect based on QGraphicsBlurEffect and using parts of this answer . If you know any better solutions, let me know.

qgraphicsgloweffect.h:

 #pragma once #include <QGraphicsEffect> #include <QGraphicsBlurEffect> #include <QGraphicsColorizeEffect> #include <QGraphicsPixmapItem> #include <QGraphicsScene> #include <QPainter> class QGraphicsGlowEffect : public QGraphicsEffect { public: explicit QGraphicsGlowEffect(QObject *parent = 0); QRectF boundingRectFor(const QRectF &rect) const; void setColor(QColor value); void setStrength(int value); void setBlurRadius(qreal value); QColor color() const; int strength() const; qreal blurRadius() const; protected: void draw(QPainter* painter); private: static QPixmap applyEffectToPixmap(QPixmap src, QGraphicsEffect *effect, int extent); int _extent = 5; QColor _color = QColor(255, 255, 255); int _strength = 3; qreal _blurRadius = 5.0; }; 

qgraphicsgloweffect.cpp:

 #include "QGraphicsGlowEffect.h" #include <QtCore\qmath.h> QGraphicsGlowEffect::QGraphicsGlowEffect(QObject *parent) : QGraphicsEffect(parent) { } void QGraphicsGlowEffect::setColor(QColor value) { _color = value; } void QGraphicsGlowEffect::setStrength(int value) { _strength = value; } void QGraphicsGlowEffect::setBlurRadius(qreal value) { _blurRadius = value; _extent = qCeil(value); updateBoundingRect(); } QColor QGraphicsGlowEffect::color() const { return _color; } int QGraphicsGlowEffect::strength() const { return _strength; } qreal QGraphicsGlowEffect::blurRadius() const { return _blurRadius; } QRectF QGraphicsGlowEffect::boundingRectFor(const QRectF &rect) const { return QRect( rect.left() - _extent, rect.top() - _extent, rect.width() + 2 * _extent, rect.height() + 2 * _extent); } void QGraphicsGlowEffect::draw(QPainter* painter) { QPoint offset; QPixmap source = sourcePixmap(Qt::LogicalCoordinates, &offset); QPixmap glow; QGraphicsColorizeEffect *colorize = new QGraphicsColorizeEffect; colorize->setColor(_color); colorize->setStrength(1); glow = applyEffectToPixmap(source, colorize, 0); QGraphicsBlurEffect *blur = new QGraphicsBlurEffect; blur->setBlurRadius(_blurRadius); glow = applyEffectToPixmap(glow, blur, _extent); for (int i = 0; i < _strength; i++) painter->drawPixmap(offset - QPoint(_extent, _extent), glow); drawSource(painter); } QPixmap QGraphicsGlowEffect::applyEffectToPixmap( QPixmap src, QGraphicsEffect *effect, int extent) { if (src.isNull()) return QPixmap(); if (!effect) return src; QGraphicsScene scene; QGraphicsPixmapItem item; item.setPixmap(src); item.setGraphicsEffect(effect); scene.addItem(&item); QSize size = src.size() + QSize(extent * 2, extent * 2); QPixmap res(size.width(), size.height()); res.fill(Qt::transparent); QPainter ptr(&res); scene.render(&ptr, QRectF(), QRectF(-extent, -extent, size.width(), size.height())); return res; } 

Then you can use it like:

 QGraphicsGlowEffect * glow = new QGraphicsGlowEffect(); glow->setStrength(4); glow->setBlurRadius(7); ui.label->setGraphicsEffect(glow); 

The result is a good glow effect:

enter image description here

+1
source

Source: https://habr.com/ru/post/1214905/


All Articles