PDA

View Full Version : qgridlayout and hiding widgets



Michael_Levin
21st May 2010, 11:11
hi!
i have a QGridLayout which contents for example 3*3 widgets, and let the middle widget be named as 'GL_SL'.
Also i have some button , and when i press it i want to hide all widgets in gridlayout except my main - and the main should be 'maximized' to the size of gridlayout.

hiding:


for i in range(self.gridLayout.count() - 1, -1, -1):
x = self.gridLayout.itemAt(i).widget()
if x.objectName() != 'GL_LS':
x.hide()
self.gridLayout.activate()
self.opengl.adjustSize() ## self.opengl is 'GL_SL'


show:


for i in range(self.gridLayout.count() - 1, -1, -1):
x = self.gridLayout.itemAt(i).widget()
if x.objectName() != 'GL_LS':
x.show()
self.gridLayout.activate()
self.adjustSize()


and that of course isn't right, cause all stuff disappears, but the 'GL_SL' is not maximized, because there are still other widgets in gridlayout, they are only hidden now, but layout.activate and adjustSize can't help to make my center widget 'maximized' in layout.

the way i can solve the problem - add and remove widgets (instead of show and hide)..)

what is the best solution foir my problem?

thank you!

high_flyer
21st May 2010, 11:34
and that of course isn't right, cause all stuff disappears, but the 'GL_SL' is not maximized, because there are still other widgets in gridlayout, they are only hidden now, but layout.
according to the docs, this actually is not true, and should work the way you want it:

Calling QWidget::hide() on a widget also effectively removes the widget from the layout until QWidget::show() is called.

Try calling update() / updateGeometry() on the widget that contains the QGridLayout after you are hiding/showing the widgets.
Calling update() and invalidate() on the layout might also help.

Michael_Levin
21st May 2010, 13:28
thank you for your reply!
yes, i've read the docs about hide/show, but unfortunately it doesn't work..)
the only working solution is:

hide all stuff:


for i in range(self.gridLayout.count() - 1, -1, -1):
x = self.gridLayout.itemAt(i).widget()
if x.objectName() != 'GL_LS':
self.gridLayout.removeWidget(x)
x.setParent(None)

show:


self.gridLayout.addWidget(self.label1, 0, 0, 1, 2)
.... (adding all other widgets that were hidden)...
for i in range(self.gridLayout.count() - 1, -1, -1):
x = self.gridLayout.itemAt(i).widget()
x.setParent(self)

SixDegrees
21st May 2010, 17:47
Have you set your main widget's layout to the grid layout? If not, the grid layout itself won't know what to resize itself to. You have to either explicitly set the grid layout as your widget's layout, or stuff it inside yet another layout whose size is ultimately controlled by the main widget.