const qreal opaque = 1.0;
const qreal transparent = 0.5;
const QRect shadow
(10,
10,
100,
100);
const QRect item
(0,
0,
100,
100);
painter->setOpacity( transparent );
painter
->setBrush
( QBrush(Qt
::black) );
painter->drawEllipse( shadow );
painter->setOpacity( opaque );
painter->setBrush( painter->background() );
painter->drawEllipse( item );
painter->setOpacity( transparent );
painter
->setBrush
( QBrush(Qt
::red) );
painter->drawEllipse( item );
}
void paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ){
const qreal opaque = 1.0;
const qreal transparent = 0.5;
const QRect shadow(10,10,100,100);
const QRect item(0,0,100,100);
painter->setOpacity( transparent );
painter->setBrush( QBrush(Qt::black) );
painter->drawEllipse( shadow );
painter->setOpacity( opaque );
painter->setBrush( painter->background() );
painter->drawEllipse( item );
painter->setOpacity( transparent );
painter->setBrush( QBrush(Qt::red) );
painter->drawEllipse( item );
}
To copy to clipboard, switch view to plain text mode
Another way is to set proper clip path for painter when rendering shadow, so it will not be rendered in the area under item. This solution will not erase background items.
#include <QtCore>
#include <QtGui>
public:
}
}
const qreal transparent = 0.5;
const QRect shadow
(10,
10,
100,
100);
const QRect item
(0,
0,
100,
100);
QPainterPath scene; scene.
addRect(0,
0,
300,
300);
// scene rectangle, hard coded here, but its just an example shadowp = scene.subtracted(shadowp); // can render everywhere except item's area
painter->setClipPath(shadowp);
painter->setOpacity( transparent );
painter
->setBrush
( QBrush(Qt
::black) );
painter->drawEllipse( shadow );
painter->setClipPath(itemPath); // you can use setClipPath(scene) as well
painter
->setBrush
( QBrush(Qt
::red) );
painter->drawEllipse( item );
}
};
int main(int argc, char *argv[])
{
view->setScene(scene);
view->show();
RectItem * rect = new RectItem();
rect->setPos(20,20);
RectItem * rect2 = new RectItem();
rect2->setPos(40,50);
scene->addItem(rect);
scene->addItem(rect2);
return a.exec();
}
#include <QtCore>
#include <QtGui>
class RectItem : public QGraphicsItem{
public:
RectItem( QGraphicsItem * parent = NULL ) : QGraphicsItem(parent){
}
QRectF boundingRect() const{
return QRectF(0,0,150,150);
}
void paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ){
const qreal transparent = 0.5;
const QRect shadow(10,10,100,100);
const QRect item(0,0,100,100);
QPainterPath scene; scene.addRect(0,0,300,300); // scene rectangle, hard coded here, but its just an example
QPainterPath shadowp; shadowp.addEllipse(item);
shadowp = scene.subtracted(shadowp); // can render everywhere except item's area
painter->setClipPath(shadowp);
painter->setOpacity( transparent );
painter->setBrush( QBrush(Qt::black) );
painter->drawEllipse( shadow );
QPainterPath itemPath; itemPath.addEllipse(item);
painter->setClipPath(itemPath); // you can use setClipPath(scene) as well
painter->setBrush( QBrush(Qt::red) );
painter->drawEllipse( item );
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView * view = new QGraphicsView();
QGraphicsScene * scene = new QGraphicsScene(QRectF(0,0,300,300));
view->setScene(scene);
view->show();
RectItem * rect = new RectItem();
rect->setPos(20,20);
RectItem * rect2 = new RectItem();
rect2->setPos(40,50);
scene->addItem(rect);
scene->addItem(rect2);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
I think this should be what you are looking for. Probably you can use your item's
Bookmarks