Nominate our 2022 Qt Champions!

[solved] Moving and Resizing QGraphicsSvgItem with PyQt4

  • It makes a few days that I've started to play with Python3 and PyQt4.
    I thought that it could be nice to make a simple card game. I've read some tutorials, searched on PyQt4, Qt4, Qt5 and PyQt5 documentation but I got stacked!
    But now I'm starting to have some problems: how to resize my Items so they have a relational size with the scene.sceneRect() (cardHeight = sceneRect().height()/10) and how should implement independent movement to those items (separate time of movement start and target).
    I have a big file already written but here is a sample example where I would like to implement those features (if you help me).
    @from PyQt4 import QtCore, QtGui, QtSvg

    class GameView(QtGui.QGraphicsView):

    """Widget where we will see the results.
    the file used may be found at
    def __init__(self, filename = "../pictures/oxygen-air.svgz", parent = None):
        """Initialize the Viewer."""
        super(GameView, self).__init__(parent)
        carddeck = QtSvg.QSvgRenderer(filename)
        for someId in ("back", "1_spade", "queen_diamond"):
            newCard = QtSvg.QGraphicsSvgItem()
            myCards[someId] = newCard
        self.scene = QtGui.QGraphicsScene()
        # Set screen resolution in case it's shown on fullscreen mode
        tempPos = 0, 0
        for cardId in myCards.keys():
            myCards[cardId].setPos(QtCore.QPointF(tempPos[0], tempPos[1]))
            tempPos = tempPos[0] + myCards[cardId].boundingRect().width(),
                    tempPos[1] + myCards[cardId].boundingRect().height()
    def resizeEvent(self, event):
        """Only to make sure that qgraphics view have no scroolbars."""
        self.fitInView(self.scene.sceneRect(), QtCore.Qt.KeepAspectRatio)

    if name == "main":

    import sys
    app = QtGui.QApplication(sys.argv)
    widget = GameView()

  • Think I've the answer to one part: Resizing.
    Here I'll leave my first approach.

    @def resize(svgItem, maxSizeRectF):

    """Resize an svgItem till fit on maxSizeRectF rectangle."""
    # Get the smaller side of the rect
    relativeSize = min( maxSizeRectF.width(),maxSizeRectF.height() )
    #Scale the item


    Now here came the part I haven't used before!
    When we need to know the measures of this Svg Item, we have to be carefull!

    svgItem."boundingRect()": - will return the svgItem's (own) bounding Rect
    we may need to use svgItem."sceneBoundingRect()": to get the bounding rect of this Item on the scene.
    If I'm wrong please correct me.
    Now it's time to find my second part of the problem... movement!

  • And think I got him! Is something like this:

    @def moveCard(self, card, targetPoint, timeLine = 5000, rotation = 0):

    """Move the card from one position to one other."""
    cardAnimation.setPosAt(1, targetPoint)
    cardAnimation.setRotationAt(1, rotation)

    ... improve speed with svg card items?

    I've noted improved speed adding @card.setCacheMode(QtGui.QGraphicsItem.NoCache)@

Log in to reply