Hiding Qt widget and keeping widget space

Viewed 24153

In a simple form I made a few buttons and put a horizontal layout. When mouse leaves the area one of the buttons, the last one, should be hidden. Using button->hide() it works, but all the buttons are rearranged by the layout manager. What I want is that all other buttons remain in their positions. I tried replacing the widget with a widget placeholder and swapping the button and placeholder hide()/show(), calling placeholder->resize(button->size()), but the layout manager doesn't respect the resize, and the placeholder is set with its minimum size. What is the best way to remove a widget and keep its space?

9 Answers

Try putting the button you want to hide and unhide in another layout. In that layout along with the button put a spacer. Call Button hide and spacer will take over.Spacer takes over hidden button's space. Other buttons continue to occupy same space.

A simple way to do it is to set the miminum size of the place holder widget instead of resizing it. While the size of the widget is not respected by layouts, the minimum size is.

Related