Important: Please read the Qt Code of Conduct -

How to have a QDockWidget with auto adjusted size?

  • Hi,
    In my application I have a QDockWidget. Inside the QDockWidget I have various widgets. Some of them changes dynamically their size.

    My problem now is when the QDockWidget is undocked. In floating mode I want that it always takes the minimum possible size. So, what I did (using the topLevelChanged() signal) is to call adjustSize() when it becomes floating. This works well.

    When I performs an action that increases the size of an internal widget, the size of the QDockWidget automatically increases too. That is what I want.

    But, when the action reduces the size of the internal widget, the size of the QDockWIdget does not follows. And this NOT what I want.

    So, do you know how to force the QDockWidget too always adjust its size to the internal content?


  • Lifetime Qt Champion


    Can you show the method's implementation ?

  • Hi @SGaist ,
    I implemented almost nothing. Currently I'm just using QDockWidget. The only code I have added is to connect the signal topLevelChanged() that calls adjustSize() on the QDockWidget when the parameter is "true". So, this reduces the size of the floating QDockWidget to the expected one. But it is just a "one-shot" operation (when the widget becomes floating).

  • Lifetime Qt Champion

    Then what can trigger a widget size reduction ?

  • Hi @SGaist,
    This is exactly my problem. As, in the QDockWidget I have a complex hierarchy of widgets, that can be changed programmatically, I do not know how to manage the fact that a widget has disappeared (removed or just hidden). I expected to have something with the size policies. But I did not found a combination of size policies that performs what I want. At least in the case of reduction size.

  • Lifetime Qt Champion

    But you do use layout in the QDockWidget via setWidget and apply layout to that
    to hold the various widgets that you hide and show etc ?

Log in to reply