这不是我自己问题的确切答案,但我想它对初学者有用。只有QGraphicsEffect::draw
成员函数需要被覆盖(margins_
是QMargins
成员变量):
void FadeEdgesEffect::draw(QPainter* painter)
{
QLinearGradient lg;
lg.setColorAt(qreal(0), QColor(0, 0, 0, 0));
lg.setColorAt(qreal(1), QColor(0, 0, 0, 255));
qreal const width(boundingRect().width());
qreal const height(boundingRect().height());
QPoint offset;
QPixmap pixmap(sourcePixmap(Qt::LogicalCoordinates, &offset));
{
QPainter p(&pixmap);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
if (margins_.left())
{
lg.setStart(qreal(0), qreal(0));
lg.setFinalStop(margins_.left(), qreal(0));
p.fillRect(QRectF(0, 0, margins_.left(), height), lg);
}
// else do nothing
if (margins_.right())
{
lg.setStart(qreal(width), qreal(0));
lg.setFinalStop(width - margins_.right(), qreal(0));
p.fillRect(QRectF(width - margins_.right(),
0, margins_.right(), height), lg);
}
// else do nothing
if (margins_.bottom())
{
lg.setStart(qreal(0), height);
lg.setFinalStop(qreal(0), height - margins_.top());
p.fillRect(QRectF(0, height - margins_.bottom(),
width, margins_.bottom()), lg);
}
// else do nothing
if (margins_.top())
{
lg.setStart(qreal(0), qreal(0));
lg.setFinalStop(0, margins_.top());
p.fillRect(QRectF(0, 0, width, margins_.top()), lg);
}
// else do nothing
}
painter->drawPixmap(offset, pixmap);
}