Reputation: 239
I need to draw many circles in sequence. When I draw next circle, the intersection with others should be hidden. Please give me an example code.
On this picture firstly I draw "1" circle, then "2" circle, then "3" circle
Upvotes: 0
Views: 4349
Reputation: 1713
Use QPainter
to do this work.
# Create a place to draw the circles.
circles = QImage( 700, 700, QImage.Format_ARGB32 )
# Init the painter
p = QPainter( circles )
# DestinationOver results in the current painting
# going below the existing image.
p.setCompositionMode( QPainter.CompositionMode_DestinationOver )
p.setRenderHints( QPainter.HighQualityAntialiasing )
p.setBrush( Qt.white )
p.setPen( QPen( Qt.green, 3.0 ) )
# Paint the images in the PROPER order: 1, 2, 3, etc
p.drawEllipse( QPoint( 300, 300 ), 200, 200 )
p.drawEllipse( QPoint( 450, 450 ), 100, 100 )
p.drawEllipse( QPoint( 300, 450 ), 150, 150 )
p.end()
# The above image is transparent. If you prefer to have
# a while/color bg do this:
final = QImage( 700 ,700, QImage.Format_ARGB32 )
final.fill( Qt.lightgray )
p = QPainter( final )
# Now we want the current painting to be above the existing
p.setCompositionMode( QPainter.CompositionMode_SourceOver )
p.setRenderHints( QPainter.HighQualityAntialiasing )
p.drawImage( QRect( 0, 0, 700, 700 ), circles )
p.end()
# Save the file.
final.save( "/tmp/trial.png" )
Note that the same code can we used for painting a widget directly. In case of widget, override its ::paintEvent( QPaintEvent* )
and do this work.
Upvotes: 1