PDA

View Full Version : how to make QGraphicsItem transparen elliptically in center



wagmare
5th August 2013, 12:30
how can i make the center part of the QGraphicsItem to be transparent so the drawbackground of scene which i made as opengl can be visible ..
i have attached the image copy of my item with the post

i did able to make a transparent item by using


QRegion circleRegion(shadow, QRegion::Ellipse); QRegion overlayRegion(m_bounds.toRect()); QRegion cropRegion = overlayRegion.subtracted(circleRegion);

painter->setClipRegion(cropRegion);

painter->drawRect(m_bounds);

but the clipRegion is not allowing other items to be seen on the top of the item . allthe items that i tried to place on the clipped regions are not visible.. but the drawBackgroundScene is visible. setZvalue also not helping me.

wysota
5th August 2013, 15:37
painter->drawRect(boundingRect());
painter->setCompositionMode(QPainter::CompositionMode_Clear );
painter->drawEllipse(boundingRect());
If it doesn't work for OpenGL viewports then first paint on a pixmap and then paint the pixmap to the item:


QPixmap px(0, 0, width(), height());
QPainter p(&px);
QRectF r = QRectF(0,0,width(), height());
p.drawRect(r);
p.setCompositionMode(QPainter::CompositionMode_Cle ar);
p.drawEllipse(r);
p.end();
painter->drawPixmap(boundingRect().topLeft(), px);

Also remember to implement shape() properly.

wagmare
6th August 2013, 15:52
painter->drawRect(boundingRect());
painter->setCompositionMode(QPainter::CompositionMode_Clear );
painter->drawEllipse(boundingRect());
If it doesn't work for OpenGL viewports then first paint on a pixmap and then paint the pixmap to the item:


QPixmap px(0, 0, width(), height());
QPainter p(&px);
QRectF r = QRectF(0,0,width(), height());
p.drawRect(r);
p.setCompositionMode(QPainter::CompositionMode_Cle ar);
p.drawEllipse(r);
p.end();
painter->drawPixmap(boundingRect().topLeft(), px);

Also remember to implement shape() properly.

thx for the reply wysota
i did what u told me


OverlayItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPixmap px( m_bounds.width(), m_bounds.height());

QPainter p(&px);
p.setBrush(QColor(38, 59 , 102));
QRectF r = shadow;
p.drawRect(m_bounds);
p.setCompositionMode(QPainter::CompositionMode_Cle ar);

p.drawEllipse(shadow);
p.end();

painter->drawPixmap(boundingRect().topLeft(), px);
}

QPainterPath OverlayItem::shape() const
{
QPainterPath path;
path.addRect(m_bounds);
return path;
}



now the other items are visible but scenes :drawBackground(QPainter *painter, const QRectF &rect)
was looking black ,
even i removed OpenGl to normal color brush yet its looking black.

please help me ..

these are the flags i set in view
setRenderHint(QPainter::Antialiasing, false);
setResizeAnchor(QGraphicsView::AnchorViewCenter);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setOptimizationFlags(QGraphicsView::DontSavePainte rState);
setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);

setViewportUpdateMode( QGraphicsView::MinimalViewportUpdate);

wysota
6th August 2013, 16:29
I don't know why your background is black, it is not related to the code posted here. Alas, your shape implementation is not correct, it should probably return a rect with a big hole in the middle. It could be the reason why the background is black...

wagmare
7th August 2013, 08:50
thx wysota for reply..
yeah i made that but in vain.. if i setOpactity before composition just a less to transparent its working fine ..
setOpacity(0.9);

wysota
7th August 2013, 09:16
Did you correct your shape() implementation?

wagmare
7th August 2013, 11:11
once again thx for reply ..
yeah i did that
QPainterPath OverlayItem::shape() const
{
QPainterPath path;
path.addRect(m_bounds);
path.addEllipse(QRectF(m_bounds.x() + 120, m_bounds.y()+20, m_bounds.height()-40, m_bounds.height()-40));
return path;
}

correct me if m wrong ..

wysota
7th August 2013, 14:34
Change your paint() implementation to something like this and see if the shape is correct or not:


void MyItem::paint(...) {
painter->drawPath(shape());
}

wagmare
7th August 2013, 15:10
thx for replying :)
yeah it came clearly ..
painter->drawPath(shape());

i have attached the screenshot with it ..9393

wysota
7th August 2013, 15:31
So is the shape correct or not?

wagmare
8th August 2013, 12:50
So is the shape correct or not?

i dont know where i was doing in wrong .. please help me .. i cant figure it out ..

wysota
8th August 2013, 14:12
I don't know what you are doing wrong. You have posted some picture and I'm asking whether it contains the shape you expected it to have. If not then correct it, if yes then state so.

wagmare
8th August 2013, 14:35
I don't know what you are doing wrong. You have posted some picture and I'm asking whether it contains the shape you expected it to have. If not then correct it, if yes then state so.

yes yes, im getting the shape which i rquired to draw .. its coming correcly .. with a circle inside ..
but its not hollow in middle ..

wysota
8th August 2013, 15:55
yes yes, im getting the shape which i rquired to draw .. its coming correcly .. with a circle inside ..
but its not hollow in middle ..

So it is not correct. It would have been correct if it had a hole inside, which as I understand it, it has not.

wagmare
9th August 2013, 08:07
So it is not correct. It would have been correct if it had a hole inside, which as I understand it, it has not.

thanks once again !
i have attached my sample code with this post .. can u please help me identifying my error..

wysota
9th August 2013, 10:16
This works fine for me:


void OverlayItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPixmap px(boundingRect().size().toSize());
px.fill(Qt::transparent);
QRect r(QPoint(0,0), boundingRect().size().toSize());
QPainter p(&px);
p.setRenderHint(QPainter::Antialiasing);
p.setBrush(Qt::red);
p.setPen(Qt::blue);
p.drawRect(r);
p.setCompositionMode(QPainter::CompositionMode_Cle ar);
p.drawEllipse(r);
p.end();
painter->drawPixmap(boundingRect().topLeft(), px);
}

wagmare
9th August 2013, 11:35
Thanks a lot ... wysota ur code works neat.
i got my mistake ... i was trying to paint a rect and then i was trying to add a pixmap on top of it with composition later ..
this one is neat and clear ..
now its working proper .. thx ..