Qgraphicseffect Shadow and Blur effect of QT
QT Qgraphicseffect Shadow Blur effect chart Shadow and blur effect normal effect code CUSTOMSHADOWEFFECTH customshadoweffectcpp Qgraphicsblureffect Fuzzy degree Setblurradius reference applying It end reference address
Effect Chart shadows and Blur effects
Normal Effect
Code customshadoweffect.h
#ifndef customshadoweffect_h
#define CUSTOMSHADOWEFFECT_H
#include <QGraphicsDropShadowEffect>
#include <QGraphicsEffect>
class Customshadoweffect:public qgraphicseffect
{
q_object
Public:
Explicit Customshadoweffect (QObject *parent = 0);
void Draw (qpainter* painter);
QRECTF boundingrectfor (const qrectf& rect) const;
inline void setdistance (qreal distance) {_distance = distance; Updateboundingrect ();}
inline qreal distance () const {return _distance;}
inline void Setblurradius (Qreal blurradius) {_blurradius = Blurradius; Updateboundingrect ();}
Inline Qreal Blurradius () const {return _blurradius;}
inline void SetColor (const qcolor& color) {_color = color;}
inline Qcolor color () const {return _color;}
Private:
qreal _distance;
Qreal _blurradius;
Qcolor _color;
};
#endif//Customshadoweffect_h
Customshadoweffect.cpp
#include "customshadoweffect.h" #include <QPainter> customshadoweffect::customshadoweffect (QObject *parent): Q Graphicseffect (parent), _distance (4.0f), _blurradius (10.0f), _color (0, 0, 0,) {} qt_begin_namespace Ext Ern q_widgets_export void Qt_blurimage (Qpainter *p, QImage &blurimage, qreal radius, bool quality, bool alphaonly, int
transposed = 0); qt_end_namespace void Customshadoweffect::d raw (qpainter* painter) {//If nothing to show outside the item, just draw
SOURCE if ((Blurradius () + distance ()) <= 0) {drawsource (painter);
Return
Pixmappadmode mode = qgraphicseffect::P adtoeffectiveboundingrect;
Qpoint offset;
Const Qpixmap px = Sourcepixmap (Qt::D evicecoordinates, &offset, mode);
return if no source if (Px.isnull ()) return;
Save World Transform Qtransform restoretransform = Painter->worldtransform ();
Painter->setworldtransform (Qtransform ()); CalCulate size for the background image qsize Szi (Px.size (). Width () + 2 * distance (), px.size (). Height () + 2 * distance ()
);
QImage tmp (Szi, qimage::format_argb32_premultiplied);
Qpixmap scaled = px.scaled (Szi);
Tmp.fill (0);
Qpainter Tmppainter (&tmp);
Tmppainter.setcompositionmode (Qpainter::compositionmode_source);
Tmppainter.drawpixmap (qpointf (-distance (),-distance ()), scaled);
Tmppainter.end ();
Blur the alpha channel QImage blurred (Tmp.size (), qimage::format_argb32_premultiplied);
Blurred.fill (0);
Qpainter Blurpainter (&blurred);
Qt_blurimage (&blurpainter, TMP, Blurradius (), false, true);
Blurpainter.end ();
TMP = blurred;
Blacken the image ... tmppainter.begin (&tmp);
Tmppainter.setcompositionmode (Qpainter::compositionmode_sourcein);
Tmppainter.fillrect (Tmp.rect (), color ());
Tmppainter.end ();
Draw the Blurred shadow ... painter->drawimage (offset, TMP); Draw the ACTual pixmap ... painter->drawpixmap (offset, PX, QRECTF ());
Restore World Transform Painter->setworldtransform (restoretransform);
} QRECTF customshadoweffect::boundingrectfor (const qrectf& rect) Const {Qreal delta = Blurradius () + distance ();
Return rect.united (rect.adjusted (-delta,-delta, Delta, Delta)); }
qgraphicsblureffect Fuzzy degree Setblurradius Reference document
applying It
Customshadow::customshadow (Qwidget *parent): Qwidget (parent) {UI.SETUPUI (this);
Ui.pushbutton->installeventfilter (this);
Ui.widget->installeventfilter (this);
Fuzzy effect Auto Blureffect = new Qgraphicsblureffect (ui.lineedit);
Blureffect->setblurradius (2);
Ui.lineedit->setgraphicseffect (Blureffect); BOOL Customshadow::eventfilter (QObject *obj, qevent *event) {if (obj = = Ui.pushbutton) {if (Event->typ
E () = = Qevent::enter) {shadow* Bodyshadow = new Shadow (Ui.pushbutton);
Ui.pushbutton->setgraphicseffect (Bodyshadow);
return true;
else if (event->type () = = Qevent::leave) {ui.pushbutton->setgraphicseffect (nullptr);
return true;
else {return false; } else if (obj = = ui.widget) {if (event->type () = = Qevent::enter) {shadow* body Shadow = new Shadow (UI. widget);
Ui.widget->setgraphicseffect (Bodyshadow);
return true;
else if (event->type () = = Qevent::leave) {ui.widget->setgraphicseffect (nullptr);
return true;
else {return false;
} else {//Pass the ' event ' to the ' parent class return __super::eventfilter (obj, event); }
}
End
Just for the record, just for sharing! Willing to write can be helpful to you. Don't forget to order a praise, thank you ~ Reference address
Qt:shadow around window