I have a QToolBox in a QDockWidget
MyWidget::MyWidget ()
: QMainWindow ()
{
auto tool_dock = new QDockWidget ();
auto tool_box = new QToolBox ();
tool_box->addItem (create_draw (), tr ("Draw"));
tool_box->addItem (create_noisy_line (), tr ("Noisy Line"));
tool_box->addItem (create_flood_fill (), tr ("Flood Fill"));
tool_dock->setWidget (tool_box);
// tool_dock->show ()
addDockWidget (Qt::LeftDockWidgetArea, tool_dock)
setCentralWidget (create_central_widget ());
}
The dock widget collapses to its smallest possible width.
If I show tool_dock
as a top-level widget
tool_dock->show ()
// addDockWidget (Qt::LeftDockWidgetArea, tool_dock)
then it looks like this, which has the correct width:
but if I add it to the dock widget instead
// tool_dock->show ()
addDockWidget (Qt::LeftDockWidgetArea, tool_dock)
then it looks like this:
The "Draw" and "Flood Fill" tool box items are both empty QWidgets. This is intentional. If I click on the "Noisy Line" tool box item, the docked tool_dock
resizes to the width of the contents.
When tool_dock
is shown as a top-level widget, the width of the tool_box
fits its contents even when an empty tool box item is showing. I want this behaviour to also occur when the tool_dock
is docked.
I tried setting the horizontal size policies of both the tool box and dock to MinimumExpanding, but that didn't work.
How can I make this QToolBox always fit the width of its contents, even when some of its items are empty, and also when it is in a QDockWidget?