PDA

View Full Version : making screen follow Qgraphicsitem



Nor1
14th March 2019, 15:38
How is it possible to make screen follow QGraphicsItem?
I am trying to make a game and I need that screen follows the player, is there any class I can call or something?

d_stranz
14th March 2019, 19:13
There are many QGraphicsView methods that control what part of the scene is displayed in the view:

QGraphicsView::centerOn()

QGraphicsView::ensureVisible()

QGraphicsView::fitInView()

QGraphicsView::setSceneRect(), etc.

One of these will probably do what you need.

Nor1
15th March 2019, 07:27
I am trying to work with centerOn, but can't make it work :s..

My code looks like this and the centerOn is not doing anything.


Creating scene:


self.scene = QtWidgets.QGraphicsScene()
self.scene.setSceneRect(0, 0, 700, 700)
self.view = QtWidgets.QGraphicsView(self.scene, self)



Creating rect, that player can control:


self.item = QtWidgets.QGraphicsRectItem(-300, 500, 50, 50)
self.item.setBrush(QtGui.QBrush(QtGui.QColor(color[0], color[1],color[2]), QtCore.Qt.SolidPattern))
self.scene.addItem(self.item)
self.newplayer = Player(color, self.starting, self.item, self.view)

And in Players class init function I am trying to call:


self.view.centerOn(self.item)