How to automatically position dock widgets?
-
Hello everyone, hope all is well! Currently, I am working on a function that displays a dock widget once a push button is clicked. I had no problems getting that working. However, I would like to be able to set the size and the position of the docks when they are created. Even more specifically, I also have a function that displays four docks when another button is clicked. My goal for this specific function is to display four dock windows as shown here:
However, I have only been able to make such a layout by manually doing it once the app is running. I would like users to have this performed for them automatically as soon as they click the button. I am currently using the ui editor to implement my dock widgets, hiding them in the program, and then (in the program) showing them once a certain button is clicked.
In short, my main question is: how do you set the size and position of a dock widget when it first appears on screen.
-
@WesLow
Hi
Ok. just asking as docked Docks can be moved. but when floating they are just windows.- nope it simply moves the widget around on the screen. like if you dragged them there.
- move can do it. it's much like setGeo except it don't requires you to specify width, height also.
-
@WesLow
Hi
Are those floating DockWidgets?
You can use move() and resize()
or setGeometryThere is also support in MainWindow for storing a state
https://forum.qt.io/topic/69899/can-i-save-restore-the-current-state-pos-size-of-a-qdockwidget/3
-
@mrjj Thank you for your response! I believe they are floating dock widgets but default.
Two Follow-Up Questions:
- Do the setGeometry/move methods make the docks unable to change size after they are called? I would still like the docks to be movable/resizable after they are initially sized/placed.
- Let's say I want to position two of these widgets in the top dock window and two in the bottom dock window? Does move() accomplish this? Or is there another method that I need to call?
-
@WesLow
Hi
Ok. just asking as docked Docks can be moved. but when floating they are just windows.- nope it simply moves the widget around on the screen. like if you dragged them there.
- move can do it. it's much like setGeo except it don't requires you to specify width, height also.
-
@mrjj Thank you again! One last question as far as the reset/setGeometry functions go: is there a way to set the width of the dock widget to be equal to half the size of its parent window? For example, could you call something like dockWidget->setWidth(MainWindow/2)? Once again, thank you for all of your help!
-
@WesLow
hi
yes something likeint hw = MainWindow->width() /2; dockWidget->resize( hw, dockWidget->height() );
-
@mrjj Having trouble with two things:
- Calling MainWindow. Should I call this instead?
- Seeing the resize method work at all. Is this because the dockWidgets are within dockWidget areas?
Thank you, in advance, for your time, help, and consideration!
-
@WesLow
Hi- well if you are inside a MainWindow function, then no need to use this or the variable name
int hw = width() /2;// ( same as this->width() ) dockWidget->resize( hw, dockWidget->height() );
- well if the Docks are docked. (versus floating as windows)
then resize wont do anything as such.
In this case, the size of the internal widget often gives the size of the DockWidget.
Maybe im not sure what you ask here.
Can you maybe show what you did ?