Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [solved] Moving and Resizing QGraphicsSvgItem with PyQt4
QtWS25 Last Chance

[solved] Moving and Resizing QGraphicsSvgItem with PyQt4

Scheduled Pinned Locked Moved Language Bindings
3 Posts 1 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tripanarium
    wrote on last edited by
    #1

    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 https://github.com/sandsmark/poker-ai/blob/master/card-deck/oxygen-air.svgz
    """
    def __init__(self, filename = "../pictures/oxygen-air.svgz", parent = None):
        
        """Initialize the Viewer."""
        
        super(GameView, self).__init__(parent)
        
        carddeck = QtSvg.QSvgRenderer(filename)
        myCards={}
        
        for someId in ("back", "1_spade", "queen_diamond"):
            
            newCard = QtSvg.QGraphicsSvgItem()
            newCard.setSharedRenderer(carddeck)
            newCard.setElementId(someId)
            
            myCards[someId] = newCard
            
        self.scene = QtGui.QGraphicsScene()
        # Set screen resolution in case it's shown on fullscreen mode
        self.scene.setSceneRect(
            QtCore.QRectF(QtGui.QDesktopWidget().screenGeometry()))
        self.setScene(self.scene)
        
        tempPos = 0, 0
        
        for cardId in myCards.keys():
            
            self.scene.addItem(myCards[cardId])
            
            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()
    widget.show()
    sys.exit(app.exec_())
    @
    

    Einstein: "God does not play dice."
    Bohr: "Einstein, stop telling God what to do... No, no, you're not thinking; you're just being logical."

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tripanarium
      wrote on last edited by
      #2

      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
      svgItem.setScale(relativeSize/svgItem.boundingRect().height())
      

      @

      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()":http://pyqt.sourceforge.net/Docs/PyQt4/qgraphicsitem.html#boundingRect - will return the svgItem's (own) bounding Rect
      we may need to use svgItem."sceneBoundingRect()":http://pyqt.sourceforge.net/Docs/PyQt4/qgraphicsitem.html#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!

      Einstein: "God does not play dice."
      Bohr: "Einstein, stop telling God what to do... No, no, you're not thinking; you're just being logical."

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tripanarium
        wrote on last edited by
        #3

        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."""
        
        cardTimer=QtCore.QTimeLine(timeLine)
        
        cardAnimation=QtGui.QGraphicsItemAnimation(self)
        cardAnimation.setItem(card)
        cardAnimation.setTimeLine(cardTimer)
        
        cardAnimation.setPosAt(1, targetPoint)
        cardAnimation.setRotationAt(1, rotation)
        
        cardTimer.start()@
        

        ... improve speed with svg card items?


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

        Einstein: "God does not play dice."
        Bohr: "Einstein, stop telling God what to do... No, no, you're not thinking; you're just being logical."

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved