Results 1 to 5 of 5

Thread: circle widgets layout

  1. #1
    Join Date
    Apr 2016
    Posts
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default circle widgets layout

    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.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: circle widgets layout

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: circle widgets layout

    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. #4
    Join Date
    Apr 2016
    Posts
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: circle widgets layout

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: circle widgets layout

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 30th April 2012, 16:17
  2. Replies: 5
    Last Post: 19th April 2010, 00:31
  3. Widgets Layout in a QToolBar.
    By cydside in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2009, 09:46
  4. Putting Widgets into a Layout
    By jcoop in forum Newbie
    Replies: 16
    Last Post: 18th April 2009, 23:00
  5. Custom widgets in layout
    By Palmik in forum Newbie
    Replies: 11
    Last Post: 26th January 2009, 13:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.