Important: Please read the Qt Code of Conduct -

Layout spacing when widgets are hidden

  • I have a widget with 3 text boxes underneath each other. All margins of this widget is set to 0. The default spacing is left at 6. I want the widget to have no spacing between the child widgets an its border. Hence the only place where there should be visible space is between textbox1 and textbox2; and textbox2 and textbox3. Everything looks perfect if all 3 text boxes are visible. The problem is that I want to dynamically hide the last text box. When I hide it, there is still some space left between textbox2 and the bottom of the parent widget. I assume this is the 6px spacing, since the margins are at 0px.

    When I hide textbox3, textbox2 is the last one in the widget. How can I remove the space underneath it? If I set the layout's spacing to 0, textbox1 and textbox2 are touching, which I don't want. I basically want all spacing to stay at 6px, except if the spacing is between a textbox and the parent widget.

  • HI, which Qt version and platform are you using?

    I can not reproduce this problem under Qt5 Windows Xp

  • Qt 5 (latest stable release) under Ubuntu.

  • It seems that QFormLayout does not remove spacing for invisible items which is something that I have just encountered in a fairly complex form layout with about 8 pairs of label and QLineEdit/QComboBox widgets each of which must be shown or hidden depending on selection of another ComboBox. The hidden controls take up space in the form layout and as a result it does not look quite right. Using a individual layouts e.g horizontal layout for each label/control pair and then stacking them with a vertical layout works but does not look good either as labels tend to be of differing lengths. Using a grid layout also looks rather horrible unless you take steps to make sure that the column widths are set appropriately. Clearly a form layout would be better but the extra unwanted spacing is a bit of a nightmare. Obviously one could remove and re-insert items into a form layout but the bottom line is that all of these workarounds are extra work and ideally when using we should be concentrating on our application logic and not having to write code to work around bugs in Qt.

    I suppose one possible option (not something I have properly considered) would be to create a QFormLayout subclass in order to "fix" the problem. It might be easier than some other workarounds. For now my dialog just has to live with the spacing issue, I'll have to get back to this one later!!

    For reference the following links refer to the same problem including an unclosed Qt bug report.

Log in to reply