[Kde-games-devel] QGraphicsView and fitInView

Pierre Maurier pierre.maurier at gadz.org
Fri Feb 20 22:48:07 CET 2009


Le vendredi 20 février 2009 21:51:55 Wolfgang Rohdewald, vous avez écrit :
> 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.

I was doing it just one time in the __init__ func. Currently my little app is 
not re-sizable.
I think I have found the solution of my problem, fitInView tries to get the 
content viewable but don't seems to rescale it if it already fit in the Widget 
(ie. if the content is smaller than the View). Am I right ?

I find a work around to my problem with a QTransform operation and some math. 
In fact it's better now, because I have a better control.

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

Anyway, thank you for that piece of python code, I might find it useful 
shortly.

Regards,

--
Pierre Maurier



More information about the kde-games-devel mailing list