Qt 5.5: Problems when dragging one QDockWidget onto another
In my application, I have a QMainWindow which initially has several docked QDockWidgets. Each QDockWidget is created as follows in the main window's constructor:
MyWidget *myWidget = new MyWidget; QDockWidget *dockWidget = new QDockWidget(tr("My Widget")); dockWidget->setWidget(myWidget); dockWidget->setObjectName("MyWidget"); addDockWidget(Qt::BottomDockWidgetArea, dockWidget);
This (mostly) works, but when one QDockWidget is dragged onto another, the bottom widget shows blue to indicate this is an accepted action, but when the mouse button is released the top widget simply disappears. Subsequently, the widget that was dropped (referred to above as the top widget) is not visible and can't be made visible using the setVisible member function.
What I expected in this case is that the two widgets would occupy the same area but become tabbed. Does this problem ring a bell for anyone?
Did you already called dockNestingEnabled ?
Sorry for the delay in getting back to you. I had not been enabling nesting, but if I add the following at the start of my MainWindow constructor, there is no change:
When I drag one docked window over top of another, the bottom window goes blue. When I let go of the mouse button, the window I was dragging simply disappears. The example in the Qt 4.8 documentation for QMainWindow indicates that the following code should add dock widgets to the main window:
QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this); dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); dockWidget->setWidget(dockWidgetContents); addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
The same documentation says:
"Two dock widgets may also be stacked on top of each other. A QTabBar is then used to select which of the widgets that should be displayed."
This is the behaviour I expected when dropping one item onto another--is there something else I have to do to enable this?
kenchan last edited by
Take a look at this QMainWindow function...
From the QMainWindow documentation
Member Type Documentation
This enum contains flags that specify the docking behavior of QMainWindow.
Identical to the animated property.
Identical to the dockNestingEnabled property.
The user can drop one dock widget "on top" of another. The two widgets are stacked and a tab bar appears for selecting which one is visible.
Each dock area contains a single stack of tabbed dock widgets. In other words, dock widgets cannot be placed next to each other in a dock area. If this option is set, AllowNestedDocks has no effect.
The two vertical dock areas on the sides of the main window show their tabs vertically. If this option is not set, all dock areas show their tabs at the bottom. Implies AllowTabbedDocks. See also setTabPosition().
When dragging the titlebar of a dock, all the tabs that are tabbed with it are going to be dragged. Implies AllowTabbedDocks. Does not work well if some QDockWidgets have restrictions in which area they are allowed. (This enum value was added in Qt 5.6.)
These options only control how dock widgets may be dropped in a QMainWindow. They do not re-arrange the dock widgets to conform with the specified options. For this reason they should be set before any dock widgets are added to the main window. Exceptions to this are the AnimatedDocks and VerticalTabs options, which may be set at any time.
This enum was introduced or modified in Qt 4.3.
The DockOptions type is a typedef for QFlags<DockOption>. It stores an OR combination of DockOption values.
I tried adding the following to my MainWindow constructor:
setDockOptions(QMainWindow::AllowTabbedDocks | QMainWindow::AnimatedDocks);
Unfortunately this doesn't change anything. When I drag one docked window onto another, while I'm dragging the target window shows a small tab with its name. When I drop the window, though, it still disappears and can't be made visible again by calling setVisible.
Perhaps it's not too surprising that this made no difference, since this seems to be the default value for the dockOptions property--it seems like QMainWindow is trying to allow tabbed docks, but when I drop the window for some reason it disappears (and can't be made visible again) rather than becoming a tab.
In case it's helpful... Full source for the application can be found here:
I appreciate your help on this--this problem has kept me stumped for a while now.