QGraphicsView with automatic item placement

I would like to write a resource browser using QGraphicsView. This is slightly different from the examples using QGraphicsView and QGraphicsItems because I only want one scrollbar and I want the items to move automatically when the viewport is resized. For example, when the viewport width is large enough to display 4 asssets, they should be displayed like this:

aaaa
aaaa
aa

      

but when the viewport shrinks and can only contain 3 lines, it should display them like this:

aaa
aaa
aaa
a

      

I would not want me to move these assets myself and let the graphical view manage them. How is this possible?

I wrote such a thing once, but using QWidget and paintEvent, painting all the assets myself and keeping track of how many assets can be displayed per line. Could it be easier with QGraphicsView?

+2


source to share


3 answers


QGraphicsView supports layouts. What you need to do is implement your own layout manager that inherits from QGraphicsLayout.



For the required layout, take a look at the Qt flow layout example. Transforming this example will give you a QGraphicsFlowLayout. Add QGraphicsItems to this layout and set the QGraphicsView layout to this layout and that will do the trick.

+5


source


It seems to me that you want a list, not a graphical view. The list can be customized to display everything around the way you want. See the example puzzle , noting the list of puzzles on the left. It looks pretty simple to set up for the case presented.



Of course, if you really want it in graphical view, I suppose you could add a list to the view and use it there.

+1


source


I would use a custom layout for this. Try creating your own Layout class that inherits from QGraphicsLayout and controls how it lays out items.

0


source







All Articles