[Kde-games-devel] QGraphicsView and fitInView

Wolfgang Rohdewald wolfgang at rohdewald.de
Fri Feb 20 21:51:55 CET 2009


On Freitag, 20. Februar 2009, Pierre Maurier wrote:
> But when I try that : 
> self.gameView.fitInView(self.gameScene.itemsBoundingRect(),
> Qt.KeepAspectRatio)
> 
> I get something far from what I expected (The QGraphicsItem is very small and 
> have a lot of blank space around).
> 

where do you do that? I think it should be in resizeEvent.

This works for me:

class FittingView(QGraphicsView):
    """a graphics view that always makes sure the whole scene is visible"""
    def __init__(self, parent=None):
        """generate a fitting view with our favorite properties"""
        QGraphicsView.__init__(self, parent)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        vpol = QSizePolicy()
        vpol.setHorizontalPolicy(QSizePolicy.Expanding)
        vpol.setVerticalPolicy(QSizePolicy.Expanding)
        self.setSizePolicy(vpol)
        self.setRenderHint(QPainter.Antialiasing)
        self.setRenderHint(QPainter.SmoothPixmapTransform)
        self.__background = None
        self.setStyleSheet('background: transparent')
        self.setFrameShadow(QFrame.Plain)

    def resizeEvent(self, event):
        """scale the scene for new view size"""
        QGraphicsView.resizeEvent(self, event)
        if self.scene():
            self.fitInView(self.scene().itemsBoundingRect(), Qt.KeepAspectRatio)

    def mousePressEvent(self, event):
        """for debugging"""
        print 'mousepos:', self.mapToScene(event.pos())
        QGraphicsView.mousePressEvent(self, event)




-- 
Wolfgang


More information about the kde-games-devel mailing list