Important: Please read the Qt Code of Conduct -

Layout issue

  • Hi,

    I have a dialog, in that widgets are laid out using QGridLayout. QComboBox is one of the widgets in it. When I change the selection in combo, another two widgets needs to be hidden. When I hide the widgets, it causes the space between the widgets to be more, and the widgets position changes! How can I retain the position of widgets and hide the controls?


  • Without seeing the actual code or the layout in Qt Designer, it's hard to tell, but you can try to add a spacer item(s) or use setMinimumSize() or setMaximumSize() functions. ;)

  • There's a hard way and a dirty way.
    The hard (and clean) way is to create an own QLayout which does what you want, i.e. have special options for certain rows/columns that they don't collapse as soon as the widget inside disappears.

    The dirty (and easy) way is to create an event filter for the hidable widget which prevents paintEvents from happening. So you don't change its programmatic visiblity, but hack the event system.

    All in all I think there might be a better design for this: Why not leave the widgets visible but set their enabled state so they gray out instead of becoming invisible? If you plan to keep some blank space where the widgets are supposed to pop up, I think this would be a much better user interface design.

  • The dirty solution looks really dirty :)

    Writing a new layout is difficult :(

    So, instead of hiding, I shall disable them. Why I wanted to hide them is, they are not at all related to the selected item in the combobox.


  • I do not think that this should be that difficult, either in Qt Designer and later adding extra code or completely writing it in code. Creating a new layout and adding the widgets and their relationto each other shouldn't be a problem.

    And the choice between hiding and disabling really depends on the user interface and its intended purpuse. For example if it's a dialog you may want to create a shape changing dialog, where hiding is the solution you may prefer. But on a form, it's usually better to disable. It really depends on the use.

Log in to reply