tags:

views:

140

answers:

3

I have a button followed by a QGridLayout full of widgets. I want to show/hide qgridlayout at every button click, but reading documentation of QGridlayout I see there's no show()/hide() implementation, also no setVisible() method available.

How do I achieve this?

+2  A: 

You didn't mention which version of Qt you're using. (I'm looking at the 4.4 documentation.)

I haven't tried this, but here are two ideas:

  • 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.
Bill
I'm using 4.6.1, and I already tried that widget()->hide() method, but throwed a segment fault. Maybe I got something wrong
clinisbut
@clinisbut: Did you test the result of `widget()` to see if it's valid? It may return `NULL` / `0`.
Bill
+4  A: 

Layouts only affect the size/position of the widgets added to them - for visibility (and anything else - event handling, focus, enable+disable) you care about the parent widget, as mentioned above. QLayout::parentWidget() gives you the widget which owns the layout, which you can then show and hide.

James Turner
So you suggest to wrap the qlayout in a qwidget to be able to hide/show() isn't it?
clinisbut
Thank you for the clarification on how Layouts work. I haven't used them before, so I'm just guessing based on the documentation.
Bill
+3  A: 

I assume you have multiple QGridLayout instances, only one should be visible based on the button that has been clicked. You can use a QStackedWidget for this:

The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.

Then, for each widget in the QStackedWidget you should associate a separate QGridLayout.

See the Qt documentation for more details

Ton van den Heuvel