How to pan beyond the scrollbar range in a QGraphicsview?
Asked Answered
A

3

9

I am building a node graph in a QGraphicsView and I am currently implementing panning. I used the following question "how to pan images in QGraphicsView" to start but the panning is limited by the scrollbar range.

I also tried the translate method but it gives the same result. The view is limited to a certain rectangle.

I would like to pan without limits, the graph can becomes quite large and it is useful to be able to work in different area of the scene (one graph here, another graph over there, etc).

Anlace answered 6/10, 2013 at 9:5 Comment(4)
QGraphicsView can be used to visualize a whole scene, or only parts of it. The visualized area is by default detected automatically when the view is displayed for the first time (by calling QGraphicsScene::itemsBoundingRect()). To set the visualized area rectangle yourself, you can call setSceneRect(). This will adjust the scroll bars' ranges appropriately. Note that although the scene supports a virtually unlimited size, the range of the scroll bars will never exceed the range of an integer (INT_MIN, INT_MAX). qt-project.org/doc/qt-5.0/qtwidgets/qgraphicsview.htmlTyphoid
@TitoTito thank you for the code hint and encouragement. But, just to be clear, except setting a huge rect for the scene, there is no obvious way to center the view on any node of the graph ? For example, the first node of a big graph is likely to be close to the scene rect border so I will need to set a rect which is three times the graph bounding box to be able to center on any node. There is no easier way ?Anlace
@LionelBarret: Just think about it: the scrollbars need to have a finite range. That range has to come from somewhere. You do not need to set a "huge" rect. You merely need to set a rect that bounds all viewport-mapped-to-scene rectangles that have been centered on each node. Then it'll be exactly big enough to let you center any node, and no bigger. The alternative solution implemented usually in cell-based spreadsheets is to expand the range of the "scene" (spreadsheet) when you scroll past the end of the current "scene". You'd need to customize the QSceneView for that.Kurtzig
Thank you. I understand the scrollbars limits, I was more wondering if it was possible to have simply a 2d space without them at all (visually & functionnaly). Space partitionning and the rest would work the same.Anlace
K
11

If you take a look at this video, at the 3 minute mark you'll see the demonstration panning the screen. The application here is one I developed and although it doesn't show it, the real estate of the board appears limitless when panning.

What I did for this was to create a QGraphicsScene of 32000 x 32000 and start the application with the view at the centre of the QGraphicsScene. The test team spent ages trying to pan to the edge of the graphics scene and everyone gave up before getting there - perhaps the scene could have been smaller!

The scroll bar policies are set to off and translation is done by moving the QGraphicsView via its translate function, passing in the delta of either touch, or mouse movement that is applied in the mouseMoveEvent.

Done this way, you need not worry about exceeding the scroll bar range and there was no problem creating a very large QGraphicsScene as it's just a coordinate space.

Kamasutra answered 7/10, 2013 at 8:35 Comment(1)
Thank you, this is the kind of answer I was looking for.Anlace
B
1

I came across the same issue. However, setting the scene to something big and leaving it I do not think is the best option. I have developed a dynamic way of changing the scene size so it lets you move freely. You can find it in this other stack overflow answer.

Bonded answered 7/3, 2019 at 11:59 Comment(0)
R
-5

You want to plot graphs. Try out this Qt library - QCustomPlot , it will save you hours of hard work.

Rude answered 6/10, 2013 at 19:56 Comment(1)
This is irrelevant to the question asked. A graph and a chart are two different things.Kurtzig

© 2022 - 2024 — McMap. All rights reserved.