PDA

View Full Version : Custom layout which allows child widgets to be moved and re-positioned



sQaT
3rd December 2020, 06:03
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.

13575

13574


13576.

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 ?

Cruz
3rd December 2020, 11:46
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.

sQaT
3rd December 2020, 20:03
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?

d_stranz
3rd December 2020, 21:41
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.

sQaT
4th December 2020, 05:00
Thank you for the explanation, is very helpful, and also for the suggestion, I'm looking into the QGraphicScene and GraphicView.