Skip to content

How do I programmatically add a dock widget to a dock container widget? #394

@BenHetherington

Description

@BenHetherington

Hope you don't mind me posing this as a question.

I'm using ADS in a full-screen multi-monitor app, and to allow dock widgets to be docked into each monitor, I've set it up so that a CDockContainerWidget is created for each monitor, passing the dock manager into its constructor1.

This works great for letting the user dock widgets into each monitor, but I'm struggling to work out how to programmatically add dock widgets to these containers, in order to add a 'default' widget for each monitor.

What I've tried thus far, in case it helps
  • CDockContainerWidget::addDockWidget() seems to be the obvious API to do this, and it indeed works, except that the dock manager's map of docked widgets isn't updated to include the new one, causing, e.g. deserialisation to break.
  • The only place that seems to update this map (and emit a dockWidgetAdded() signal) is CDockManager::addDockWidget() (and its related methods within the same class). However, this defaults to using the dock manager as the container, which can only be overridden by passing it a CDockAreaWidget that already exists inside the intended container – and as no dock widgets have been added to the container yet, the container doesn't contain any dock area widgets yet, and so I don't have one available to pass in.
    • Additionally, this method just forwards on to CDockContainerWidget::addDockWidget(), which makes me unsure if the method on CDockContainerWidget is intended to be used publicly…
  • I tried working around this by calling CDockManager::addDockWidgetFloating() followed by CDockContainerWidget::addDockWidget() with the same dock widget, which will add it to the map, and add it to the dock container, deleting the floating dock container in the process. However, this apparently causes memory-related bugs within the library (namely, a pointer within d->UninitializedFloatingWidgets becomes dangling, potentially crashing the application inside CDockManager::showEvent()). While this feels like a bug within the library to me, this kind of workaround is bound to be fragile, so I'd rather not have to do something like this if it's possible.
    • Honestly, this is the approach I had implemented, until I became aware of the memory bug; in hindsight, I probably should've instead posed this question sooner, but ah well 🤦

Hopefully I've not missed something, but is there an intended way to add dock widgets to a dock container, such that the dock manager is made aware of the added dock widget? Or if there isn't currently one, is it worth adding a new addDockWidget()-style method to CDockManager that allows you to specify the container that the widget should be added to?

Footnotes

  1. Technically, as CDockManager is a subclass of CDockContainerWidget, one of the monitors uses the CDockManager as its container, but I digress…

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions