QToolBox in QDockWidget collapses to minimum size

523 Views Asked by At

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:

enter image description here

but if I add it to the dock widget instead

    // tool_dock->show ()
    addDockWidget (Qt::LeftDockWidgetArea, tool_dock)

then it looks like this:

enter image description here

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.

enter image description here

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?

0

There are 0 best solutions below