Results 1 to 5 of 5

Thread: Custom layout which allows child widgets to be moved and re-positioned

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2020
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question Custom layout which allows child widgets to be moved and re-positioned

    I am looking for a way to have a bunch of childs widgets(buttons), contained inside a main window widget/layout, to be able to move them around and re-position them interactively, as well as retaining their relative position to the main window scale .

    So far i could achieve something like this in a widget ui, without adding a layout (i can move any widgets etc), alto the issue is that when scaling the main UI window, all the buttons inside are staying "in place", and not moving relative to the window size, hence , falling outside of the window boarder.
    I know that adding a layout would allow the buttons to be in line with the widget scale, alto, it would also break the buttons positioning which is what i need.

    qt_01.png

    qt_02.png


    qt_03.png.

    So I would like instead to be able to , place a few button widgets inside the main window, in any position i want, and have them stay in the relative position as i rescale the main UI widget.

    After rescaling or moving the main window widget, i want to still be able to "edit" these buttons position/scale (by dragging them around ), and so on so forth.


    Any ideas on how i could achieve this or what would be the best approach to go about it ?
    Last edited by sQaT; 3rd December 2020 at 06:10.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    377
    Thanks
    95
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom layout which allows child widgets to be moved and re-positioned

    You could follow through with your current approach and leave the widgets in a main window without a layout and then recompute their positions in the resizeEvent(). You could use the mousePressEvent() and the mouseMoveEvent() to implement your dragging functionality. However, if you actually want the buttons to scale in size including the font and everything, you might have to use the Graphics View Framework to set up a QGraphicsScene where the buttons are QGraphicsWidgets and then use the scale() function of the QGraphicsScene.

  3. #3
    Join Date
    Dec 2020
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom layout which allows child widgets to be moved and re-positioned

    Thank you for the reply, i wasn't sure was a "good thing to do", not having a layout, my only concern was the speed vs amount of child widgets to move around, alto perhaps is a non-issue.
    I will look also into the graphicScene, thanks for the suggestion.

    At some point, i thought of using a grid layout, and make the grid cells small enough to allow for the buttons to be able to get into a "row, column" of choice allowing for 'some empty" cells between the different widgets, although i have difficulties on finding a way to get the column/row number given the mouse click position, perhaps is not possible?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,938
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Custom layout which allows child widgets to be moved and re-positioned

    At some point, i thought of using a grid layout
    Unfortunately, grid layouts are usually smarter than we are, and will adjust rows and columns as things get moved around. The grid layout will also make all the cells in the row and column occupied by the widget the same height and width respectively as the dropped widget, which will probably result in all of the other widgets getting pushed around into new positions as their cell positions change.

    If you want free-form movement of widgets around a window with smart scaling and repositioning on resize without having to do all the calculations yourself, I second Cruz's suggestion to use the Graphics View Framework to implement what you want in a QGraphicsScene.

    It would also give you the advantage that you could implement your own graphics objects that respond to clicks and drags without being limited to rectangular push button shapes, sizes, colors, and fonts or require you to implement the drag behavior yourself.
    <=== 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.

  5. #5
    Join Date
    Dec 2020
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom layout which allows child widgets to be moved and re-positioned

    Thank you for the explanation, is very helpful, and also for the suggestion, I'm looking into the QGraphicScene and GraphicView.

Similar Threads

  1. Custom Container with predefined child widgets
    By nibedit in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2014, 08:50
  2. Replies: 0
    Last Post: 30th April 2012, 15:17
  3. List of child widgets includes layout
    By JovianGhost in forum Qt Programming
    Replies: 5
    Last Post: 11th May 2010, 03:22
  4. Replies: 5
    Last Post: 18th April 2010, 23:31
  5. Custom widgets in layout
    By Palmik in forum Newbie
    Replies: 11
    Last Post: 26th January 2009, 12:08

Tags for this Thread

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.