gruszczy
gruszczy

Reputation: 42168

QGraphicsView with automatic items placing

I would like to write an asset browser using QGraphicsView. It's a little different from examples using QGraphicsView and QGraphicsItems, because I want only one scrollbar and I want items to move automatically, when the viewport size changes. For example, when viewport width is large enough to display 4 asssets, they should be displayed like this:

aaaa
aaaa
aa

but when viewport is shrinked and can only contain 3 in a row, it should display them like this:

aaa
aaa
aaa
a

I wouldn't like to have to move those asset's by myself and let the graphics view manage them all. Is it somehow possible?

I have written once such a thing, but using QWidget and paintEvent, drawing all assets myself and keeping track of how many assets can be displayed in a row. Can it be done simpler with QGraphicsView?

Upvotes: 3

Views: 2155

Answers (3)

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

It sounds to me you want a list, not a graphics view. A list can be set to display things wrapping around like you desire. See the puzzle example, paying attention to the list of puzzle pieces on the left. It looks pretty simple to set up for the case presented.

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

Upvotes: 1

Patrice Bernassola
Patrice Bernassola

Reputation: 14416

I would use a custom layout to do this. Try to create your custom Layout class that inherits from QGraphicsLayout and manage the way it is placing items.

Upvotes: -2

zweihander
zweihander

Reputation: 6295

QGraphicsView supports layouts. What you have to do is implement your own layout manager, inheriting from QGraphicsLayout.

For the layout you require, take a look at the Flow Layout example of Qt. Converting that example will give you a QGraphicsFlowLayout. Add your QGraphicsItems to this layout and set your QGraphicsView's layout to that layout, and that would do the trick.

Upvotes: 6

Related Questions