0

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?

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
spraff
  • 32,570
  • 22
  • 121
  • 229
  • I tried your code, and I found that Qt will memorize the size of docked and topleveled. I suggest you to try the signal 'topLevelChanged(bool topLevel)', and adjust the width of the tool_box. I tried but failed, wish you good luck. – 张铁男 Nov 27 '18 at 12:09
  • You can try set hierarchy: dock->wrapper->wrapperLayout->yourTool(s) where dock is your dock widget, wrapper is QWidget with expanding size policy (or you can set min width/height), wrapperLayout is child of wrapper, yourTool(s) is one or more tool widget(s) added to wrapperLayout. – Deep Nov 28 '18 at 12:42

0 Answers0