-1

This is a follow-up question. Here is the link to my previous question. The answer there works but the problem however I faced was that in the beginning, the line would be drawn to the mouse press taking it as the endpoint instead of been drawn from the mouse press taking it as the starting point. As suggested by a user there explicitly setting the sceneRect solves the problem, but however after explicitly setting the sceneRect the view stops adapting and showing scrollbar.

Here's a demo before adding sceneRect

enter image description here

As you could see the viewport automatically adjusts, when the mouse is over any of the edges, giving the user more space to draw. (Also note how the line is been drawn to the mouse press at the very beginning)

And Here is after if used self.setSceneRect(QtCore.QRectF(0, 0, 500, 500)) inside the constructor of the Scene class

enter image description here

As you could see it doesn't adjust the viewport itself when the mouse is near the edges of the screen unlike how it happened previously.

Now coming to my question, is there a way to make the view adapts to the changes inside the scene automatically after setting the sceneRect, or should I change sceneRect manually like shown here?

JacksonPro
  • 3,135
  • 2
  • 6
  • 29

1 Answers1

1

One possible solution is to update the sceneRect based on the size of the QGraphicsView, in addition to setting the view alignment to topleft:

class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
        self.setRenderHint(QtGui.QPainter.Antialiasing)
        self.setMouseTracking(True)
        scene = Scene()
        self.setScene(scene)

    def resizeEvent(self, event):
        super().resizeEvent(event)
        self.setSceneRect(
            QtCore.QRectF(QtCore.QPointF(0, 0), QtCore.QSizeF(event.size()))
        )


def main():
    app = QtWidgets.QApplication(sys.argv)
    view = GraphicsView()
    view.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

If you want to observe the scrollbars then you can calculate the maximum between the previous sceneRect and the sceneRect based on the size of the viewport:

class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
        self.setRenderHint(QtGui.QPainter.Antialiasing)
        self.setMouseTracking(True)
        scene = Scene()
        self.setScene(scene)

    def resizeEvent(self, event):
        super().resizeEvent(event)
        r = QtCore.QRectF(QtCore.QPointF(0, 0), QtCore.QSizeF(event.size()))
        self.setSceneRect(self.sceneRect().united(r))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I still don't see the scrollbar. Is there something I am missing? – JacksonPro Jan 19 '21 at 16:45
  • @JacksonPro Why do you want scrollbars? Please explain better what you want to obtain and do not link the comments of other posts since these are second class users in SO so they can be deleted at any time making your post unusable – eyllanesc Jan 19 '21 at 16:47
  • As the user touches the edges I want the user to be able to go beyond the boundaries as shown in the first image. In the first image, you could see that as the mouse goes towards the edges it increases the view. As for the link in the question, should I screenshot the comments and paste them here? or pls tell me how can I improve this question. – JacksonPro Jan 19 '21 at 16:54
  • @JacksonPro 1) What do you mean by "touches the edges"? Explain yourself better. 2) You should not take a screenshot or anything like that since as I pointed out they can be deleted, instead explain what you want to obtain assuming that these comments do not exist. Maybe what I point out musicamante is not what you want, so it is better that you explain what you want to obtain and maybe you have an XY problem. 3) Have you tried my second code? – eyllanesc Jan 19 '21 at 16:58
  • Yes, your second does show a scroll bar but not when the mouse touches the edges. I am too tired, I'll explain this question better after a few hours of rest. – JacksonPro Jan 19 '21 at 17:01
  • @JacksonPro 1) Please use the numbering to answer me and thus make an easy follow-up of the conversation, 2) So far you have not answered point 1 of my previous comment – eyllanesc Jan 19 '21 at 17:03
  • 4) Touch the edges, take for example the screen is 500x500 px then if my mouse is over the 500px then I want the viewport to give me extra space it doesn't mean that screen should change the size all I want is to increase the sceneRect giving the user extra space to draw. – JacksonPro Jan 20 '21 at 03:14