tags:

views:

543

answers:

6

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 hided. Using button->hide() it works, but all the buttons are rearranged by the layout manager. What I want is that all other buttons continue in their positions. I tried replacing the widget with a widget place holder and swapping the button and placeholder hide()/show(), calling placeholder->resize(button->size()), but the layout manager don't respect the resize and the placeholder is set with its minimum size. What is the best way to remove a widget and keeping its space?

A: 

I don't know if is the best way, but solved this using a QStackedWidget to wrap the button.

Humberto Pinheiro
A: 

You could try putting the button to hide inside another widget, and try to make the other widget keep its size properly when the button is hidden. This might be a little difficult. A QStackedWidget has been suggested, and might be the way to go... it would be similar to the QWidget wrapper, except sizing might be easier.

Another option you could look at is inserting a spacer for the placeholder, and setting the spacer to a fixed size that is the button's size. (You'll need to update the size when the button's size changes). You should be able to show/hide those in pairs and make it work like you expect.

Caleb Huitt - cjhuitt
+1  A: 

I would also try installing an event filter that overrides the original paint event, drawing a transparent background, and ignores most of the events that the widget should not respond to when it's invisible (probably all of them, except QResizeEvent). When the widget becomes visible again, remove the event filter.

See QObject::installEventFilter(QObject*) and QObject::removeEventFilter(QObject*).

andref
A: 

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.

Ankur Gupta
A: 

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.

shoosh
A: 

I would subclass QPushButton and modify the paintEvent so that nothing gets painted if the button is disabled. Then you can just disable the button instead of hiding it.

Since the button is still there, it'll still be respected in the layout, but because it's disabled, it'll ignore all user interaction. Of course, this won't work if you ever need the button to be both visible and disabled.

Parker