本文介绍了Qt:窗口周围的阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以添加阴影到小部件:

  QGraphicsDropShadowEffect * bodyShadow = new QGraphicsDropShadowEffect; 
bodyShadow-> setBlurRadius(9.0);
bodyShadow-> setColor(QColor(0,0,0,160));
bodyShadow-> setOffset(4.0);
ui-> widget-> setGraphicsEffect(bodyShadow);

但是这个阴影只会在左边和下边。我需要阴影周围的部件。如何添加?

解决方案

您可以创建自定义效果。



<$ p $

p> #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

  #includecustomshadoweffect.h
#include< QPainter>
// #include< QGraphicsEffect>

CustomShadowEffect :: CustomShadowEffect(QObject * parent):
QGraphicsEffect(parent),
_distance(4.0f),
_blurRadius(10.0f),
_color(0,0,0,80)
{
}

QT_BEGIN_NAMESPACE
extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter * p,QImage& blurImage,qreal radius ,bool quality,bool alphaOnly,int transposed = 0);
QT_END_NAMESPACE

void CustomShadowEffect :: draw(QPainter * painter)
{
//如果在项外没有显示,只需绘制源
if ((blurRadius()+ distance())< = 0){
drawSource(painter);
return;
}

PixmapPadMode mode = QGraphicsEffect :: PadToEffectiveBoundingRect;
QPoint offset;
const QPixmap px = sourcePixmap(Qt :: DeviceCoordinates,& offset,mode);

//返回如果没有源
if(px.isNull())
return;

//保存世界变换
QTransform restoreTransform = painter-> worldTransform();
painter-> setWorldTransform(QTransform());

//计算背景图像的大小
QSize szi(px.size()。width()+ 2 * distance(),px.size()。height * 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();

//模糊Alpha通道
QImage blur(tmp.size(),QImage :: Format_ARGB32_Premultiplied);
blurred.fill(0);
QPainter blurPainter(& blurred);
qt_blurImage(& blurPainter,tmp,blurRadius(),false,true);
blurPainter.end();

tmp = blurred;

//黑色图像...
tmpPainter.begin(& tmp);
tmpPainter.setCompositionMode(QPainter :: CompositionMode_SourceIn);
tmpPainter.fillRect(tmp.rect(),color());
tmpPainter.end();

//绘制模糊的阴影...
painter-> drawImage(offset,tmp);

//绘制实际的像素图...
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));
}

将其应用于图形项目

  // ... 
CustomShadowEffect * bodyShadow = new CustomShadowEffect();
bodyShadow-> setBlurRadius(20.0);
bodyShadow-> setDistance(6.0);
bodyShadow-> setColor(QColor(0,0,0,80));
item-> setGraphicsEffect(bodyShadow);
// ...

应用

  // ... 
CustomShadowEffect * bodyShadow = new CustomShadowEffect
bodyShadow-> setBlurRadius(20.0);
bodyShadow-> setDistance(6.0);
bodyShadow-> setColor(QColor(0,0,0,80));
ui-> widget-> setAutoFillBackground(true);
ui-> widget-> setGraphicsEffect(bodyShadow);
// ...

结果:


I can add shadow to widget:

QGraphicsDropShadowEffect *bodyShadow = new QGraphicsDropShadowEffect;
bodyShadow->setBlurRadius(9.0);
bodyShadow->setColor(QColor(0, 0, 0, 160));
bodyShadow->setOffset(4.0);
ui->widget->setGraphicsEffect(bodyShadow);

But this shadow will be only at left and down. I need shadow around widget. How to add this?

解决方案

You can create your custom effect. I'll share an implementation that works.

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>
// #include <QGraphicsEffect>

CustomShadowEffect::CustomShadowEffect(QObject *parent) :
    QGraphicsEffect(parent),
    _distance(4.0f),
    _blurRadius(10.0f),
    _color(0, 0, 0, 80)
{
}

QT_BEGIN_NAMESPACE
  extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0 );
QT_END_NAMESPACE

void CustomShadowEffect::draw(QPainter* painter)
{
    // if nothing to show outside the item, just draw source
    if ((blurRadius() + distance()) <= 0) {
        drawSource(painter);
        return;
    }

    PixmapPadMode mode = QGraphicsEffect::PadToEffectiveBoundingRect;
    QPoint offset;
    const QPixmap px = sourcePixmap(Qt::DeviceCoordinates, &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));
}

Applying it (to a graphics item):

// ...
CustomShadowEffect *bodyShadow = new CustomShadowEffect();
bodyShadow->setBlurRadius(20.0);
bodyShadow->setDistance(6.0);
bodyShadow->setColor(QColor(0, 0, 0, 80));
item->setGraphicsEffect(bodyShadow);
// ...

Applying it (to a child widget):

//...
CustomShadowEffect *bodyShadow = new CustomShadowEffect();
bodyShadow->setBlurRadius(20.0);
bodyShadow->setDistance(6.0);
bodyShadow->setColor(QColor(0, 0, 0, 80));
ui->widget->setAutoFillBackground(true);
ui->widget->setGraphicsEffect(bodyShadow);
// ...

Result:

这篇关于Qt:窗口周围的阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:24