PDA

View Full Version : circle widgets layout



sunburns
4th January 2018, 21:17
I have created a widget that looks like a circle (using QPainterPath) and I want to put several of them into another widget, in a circle, as shown in the attachment. Kind of a flower-looking thing. I'm looking for advise on the best way to do this. I'm using a QGridLayout where I set a stretch value of 1 for 15 columns and 15 rows. Then I add each circle widget at a certain row/column, and set the span for both column and row to 5. The numbers 5 and 15 were picked pretty randomly. When I resize it, most of the time it looks ok, but not always. I think the reliance on the numbers 5 and 15 are what makes it look ok at some sizes, not so good at others. Is there a better way to do this? I feel like I'm trying to force the layout to work, but it seems so close to what a grid layout should do. Would I be better off creating my own layout? Or should I not use a layout and do everything myself in the resizeEvent() method? Thanks.

d_stranz
4th January 2018, 21:46
Relying on magic numbers is only good up to a point, as you have seen. I doubt that you can get the appearance you want using the standard QLayout types.

Have you considered doing this using the Qt Graphics / View system? Your circles could be QGraphicsEllipseItem and you can control their placement in the scene precisely using a little bit of trigonometry. If you do not want the circles to change size as the view size changes, then you can set them to ignore transformations. However the radial positions of the circles could still expand outward or shrink inward with viewport scale changes.


Or should I not use a layout and do everything myself in the resizeEvent() method?

Yes, the other option is to not use a layout at all, but to make a parent container widget that holds the circle widgets as children. Based on the size of the parent container, you can move the child widget circle positions. You'll probably only need to override the resizeEvent() and maybe add some methods to set the center circle widget and to set radial circle widgets.

ChrisW67
6th January 2018, 08:31
Another option might be a custom layout manager that does the geometry calculation more exactly. Have a look at the Flow Layout Example for the general mechanism.

sunburns
10th January 2018, 00:29
Thanks for the suggestions. I created a custom layout (FlowerLayout) that mimics closely what's in the FlowLayout example. I still have a few magic number in the doLayout() method, mostly for spacing, but I'm working on getting rid of those. Thanks again - I would have spent a lot of time trying to force one of the other layouts to work.

d_stranz
10th January 2018, 16:18
That's one of the very nice things about Qt. If you don't find something in the box that works for you, the framework makes it very easy to write your own and it just works.