Graphics and effects

We could create any custom graphics that we like by creating a custom widget and by reimplementing its paint event. This approach is very helpful when we are trying to create some small graphics, such as drawing graphs or to draw basic shapes. In order to create animations and more complex graphics, we will take help from the PySide's graphics view classes, which are explained as follows:

  • QGraphicsScene: This provides a surface to manage a large number of 2D graphical items
  • QGraphicsItem: This serves as the base class for all graphical items, such as ellipses, lines, and so on, in a graphics scene
  • QGraphicsView: This provides a widget to display the contents of a graphics scene

The graphic view classes can be used by creating a scene that is represented by a QGraphicsScene object first. Scenes can be associated with the QGraphicsView object to represent or view on the screen. Items that are represented by the QGraphicsItem object can be added to the scene. A scene is created, items are added, and visualized, in that order. QGraphicsView can be triggered to visualize a whole scene or only a part of it by changing the bounding rectangle values. These interactions can happen using the mouse or a keyboard. The graphics view translates the mouse and key events into scene events represented by QGraphicsSceneEvent and forwards them to the visualized scene. The custom scene interactions are achieved by reimplementing the mouse and key event handlers. The most interesting feature of the graphics views is that we can apply transformations to them, such as scaling and rotation. This can be done without changing the original scene's items:

class MyView(QGraphicsView):
  def __init__(self):
    QGraphicsView.__init__(self)

    self.myScene = QGraphicsScene(self)
    self.myItem = QGraphicsEllipseItem(-20, -10, 50, 20)
    self.myScene.addItem(self.myItem)
    self.setScene(self.myScene)

    self.timeLine = QTimeLine(1000)
    self.timeLine.setFrameRange(0, 100)
    self.animate = QGraphicsItemAnimation()
    self.animate.setItem(self.myItem)
    self.animate.setTimeLine(self.timeLine)

    self.animate.setPosAt(0, QPointF(0, -10))
    self.animate.setRotationAt(1, 360)

    self.setWindowTitle("A Simple Animation")
    self.timeLine.start()

This program will create an animated ellipse as its output. As discussed, we created a scene, added items to it, and displayed it via the view class. The animation is supported by the QGraphicsAnimationItem() object. Many more complex animations can be built on top of this but explaining them is beyond the scope of this book. You can explore them by yourself in order to create more QGrapicsView objects and complex animations.

The PySide.QtGui.QGraphicsEffect class serves as the base class for the graphical effects on an application. With the help of effects, we can alter the appearance of elements. The graphical effects objects operate between the sources, for example, a pix map item and the destination—the viewport, and render the respective effects for the image. The various graphical effects that Qt provides are as follows:

  • QGrpahicsBlurEffect: This blurs the item by a given radius
  • QGraphicsDropShadowEffect: This renders a drop shadow behind the item
  • QGraphicsColorizeEffect: This renders the item in shades of any given color
  • QGraphicsOpacityEffect: This renders the item with a specified opacity

All these classes are inherited from the QGraphicsEffect base class. The following code excerpt shows an example of adding effects to the items:

    self.effect = QGraphicsDropShadowEffect(self)
    self.effect.setBlurRadius(8)
    self.myItem.setGraphicsEffect(self.effect)
    self.myItem.setZValue(1)

When this effect is added, you will notice a shadow in the animated ellipse, as shown in the following screenshot. Similarly, other effects can be added to the items.

Graphics and effects
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.33