Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Disabling QGraphicsScene's automatic focus



  • Hi, I'm coding a simple PyQT5 application for a little university project. It is supposed to draw objects on a scene and allow you to apply geometric transformations to them. Almost everything works fine, except that the scene showing the objects is not supposed to move, even if one of its objects gets translated/rotated/scaled; every time I apply one transformation, the scene moves to the object's direction.

    I built the GUI using QtDesigner. That's it's basic structure (I suppressed some unrelated code):

    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            # ...
            MainWindow.setGeometry(0,0,1111, 710)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            # ...
            self.shapesScene = QtWidgets.QGraphicsScene()
            self.shapesViewer = QtWidgets.QGraphicsView(self.centralwidget)
            # ...
    

    I instantiate the GUI in the main script like this:

    app = QtWidgets.QApplication([])
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()
    

    The objects are drawn in Ui_MainWindow.shapesScene using the methods addLine and addPolygon. Is there anyway I can set a static behavior to QGraphicsScene so that is keeps fixed?



  • @real_mithrandir
    While you await a better answer from someone who knows better than I... :)

    I think it may be the QGraphicsView you are seeing "moving", not the QGraphicsScene. The view is a QAbstractScrollArea, so is scrolling going on? Have a look at https://doc.qt.io/qt-5/qgraphicsview.html#sceneRect-prop, https://doc.qt.io/qt-5/qgraphicsview.html#transformationAnchor-prop, and other properties in QGraphicsView to do with scrolling/moving etc.?



  • @JonB the QGraphicsScene.setSceneRect totally worked for me. Thanks!


Log in to reply