Pherrymason
Pherrymason

Reputation: 8067

How to hide completely a QGridLayout?

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?

Upvotes: 9

Views: 8666

Answers (3)

Ton van den Heuvel
Ton van den Heuvel

Reputation: 10528

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

Upvotes: 3

James Turner
James Turner

Reputation: 2485

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.

Upvotes: 7

Bill
Bill

Reputation: 14685

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.

Upvotes: 8

Related Questions