用Qt绘制特定的圆圈

时间:2016-03-25 19:49:38

标签: qt qpainter

我需要按顺序绘制多个圆圈。当我绘制下一个圆时,应该隐藏与其他圆的交点。请给我一个示例代码。

On this picture firstly I draw "1" circle, then "2" circle, then "3" circle

1 个答案:

答案 0 :(得分:0)

使用QPainter完成这项工作。

# 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" )

请注意,我们可以使用相同的代码直接绘制窗口小部件。如果是小部件,请覆盖其::paintEvent( QPaintEvent* )并执行此操作。