Important: Please read the Qt Code of Conduct -

How to disable a QGridLayout?

  • I have a QGridLayout (gl_Modules) added in a QGroupBox. By default the QGridLayout is disabled (works perfectly) and if a RadioButton is selected it should be enabled. If it looses the selection it shall be disabled again. I am unable to disable it again after enabling.

    For some reason to enable

    is not enough.
    I needed to add

    To make it work. That was a bit odd - I haven't found that in the online-examples, just by trial and error.
    Now I experience the same problem vice versa. How do I disable a QGridLayout? (Not Delete).

    On StackOverflow someone seemed to have the same issue:

    According to the answer:
    "QGridLayout inherits the function QLayoutItem::widget(). If your layout is a widget, this will return a QWidget* on which you can call show() or hide(). If your QGridLayout is not a QWidget, you can nest it within a QWidget, and you can show() / hide() that widget instead."

    But that's not true.
    QLayoutItem has no functions show() or hide() (at least not in my Version 4.7). Also QGridLayout is not a QWidget I don't want to hide the QGroupBox in which it is nested instead, it destroys the whole layout.

    What do I need to do?

  • Lifetime Qt Champion


    Since you have this in a QGroupBox why not make use of its checkable property ? It looks exactly like what you need

  • Not really... I would like to disable it completely

  • Lifetime Qt Champion

    Including the QGroupBox ?

  • No, that should stay. I just want to be able to disable the QGridLayout during runtime too.
    I can hardly believe it's just possible to disable it on set-up.

  • Lifetime Qt Champion

    Just to be sure I understand you correctly: you want to disable the content of the grid layout that is set on the QGroupBox. Is that right ?

  • It depends what you mean by

    bq. disable the content of the grid layout

    In the beginning I disable the QGridLayout by simply using @gl_Modules->setEnabled(false);@

    I just want to be able to do that again during runtime.

  • Lifetime Qt Champion

    setEnabled on a layout doesn't do what you think, it juste changes whether a layout adjust dynamically to changes.

    So if you want to disable the widget inside the layout, you should either disable the widget containing this layout, or go through each widget contained in the layout and call setEnabled on it. In the case of a QGroupBox you also have the setCheckable property so the user can disable the group box himself.

  • Moderators

    [quote author="qohelet" date="1402179136"]@gl_Modules->setEnabled(false);@

    I just want to be able to do that again during runtime.[/quote]
    This is at runtime.

    When you disable a layout in the constructor or before the widget is even shown, all containing widgets get default position and size (likely stuffed in the upper left corner of the parent), because the layout never does its thing.

    When you enable it later on, it will not readjust elements right away. It will layout the items the next time it needs to (eg. repaint, resize or, like you did - activate). Try to resize your window and you'll see it happens.

    Then, when you disable it again it will just stop be used. It doesn't mean the elements will go back to the corner or be resized. Disabling layout does not reposition elements. The only difference you'll see is that the items are no longer automatically positioned when you resize, repaint, activate etc. The easiest way to see that is to resize the window. If you make it smaller contents will be clipped.

    Disabling a layout is not the same thing as disabling a widget. The former stops positioning elements inside when demanded (again - repaint, resize, activate etc.). The latter grays out stuff inside and disables user interaction.

    If you really want to enable/disable a layout then you're already doing it. Just know that it doesn't have any visual effect (right away). You need to force the repositioning in any of the ways.

    If you want to gray out stuff then, as SGaist said - disable the box(or contents), not the layout.

  • Then is there a way just to hide the whole QGridLayout?

    As said, if I disable the QGroupBox containing the QGridLayout the whole layout of the GUI is garbage. If I disable every widget which is inside the QGridLayout you still see the items...
    Or is there a way to hide each of the widgets? (Without modifying the layout)

Log in to reply