Home > Blockchain >  How to shrink first widget in QSplitter when adding new one?
How to shrink first widget in QSplitter when adding new one?

Time:12-02

I have a QSplitter with one large widget followed by few small ones. When a new widget is added, the small ones are shrunk below their recommended sizeHint() even though the large one still has much more space than its sizeHint().

Can I change this behaviour? Do I need to use QSplitter::setSizes() to do that and if so, how to detect that some widget was added/removed/resized?

I want the widgets to be freely resizable, so using setFixedHeight() is not an acceptable solution.

For example, I have two widgets with preferred height 200px (first screenshot). When I add the third one, it takes more than preferred height, shrinking the others even though all could have their preferred height.

Before adding 3rd widget

After adding 3rd widget

CodePudding user response:

Try QSplitter::setStretchFactor(index, factor);. For example set factor=0 for first widget and factor=1 for rest of the widgets.

You can install event filter and filter for QEvent::LayoutRequest to detect that some widget was added/removed, although it can be fired for other reasons too.

  •  Tags:  
  • qt
  • Related