Results 1 to 4 of 4

Thread: qgridlayout and hiding widgets

  1. #1
    Join Date
    May 2010
    Posts
    9
    Qt products
    Platforms
    Windows

    Default qgridlayout and hiding widgets

    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:
    Qt Code:
    1. for i in range(self.gridLayout.count() - 1, -1, -1):
    2. x = self.gridLayout.itemAt(i).widget()
    3. if x.objectName() != 'GL_LS':
    4. x.hide()
    5. self.gridLayout.activate()
    6. self.opengl.adjustSize() ## self.opengl is 'GL_SL'
    To copy to clipboard, switch view to plain text mode 

    show:
    Qt Code:
    1. for i in range(self.gridLayout.count() - 1, -1, -1):
    2. x = self.gridLayout.itemAt(i).widget()
    3. if x.objectName() != 'GL_LS':
    4. x.show()
    5. self.gridLayout.activate()
    6. self.adjustSize()
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by Michael_Levin; 21st May 2010 at 11:18.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qgridlayout and hiding widgets

    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.
    Last edited by high_flyer; 21st May 2010 at 11:41.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: qgridlayout and hiding widgets

    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:
    Qt Code:
    1. for i in range(self.gridLayout.count() - 1, -1, -1):
    2. x = self.gridLayout.itemAt(i).widget()
    3. if x.objectName() != 'GL_LS':
    4. self.gridLayout.removeWidget(x)
    5. x.setParent(None)
    To copy to clipboard, switch view to plain text mode 
    show:
    Qt Code:
    1. self.gridLayout.addWidget(self.label1, 0, 0, 1, 2)
    2. .... (adding all other widgets that were hidden)...
    3. for i in range(self.gridLayout.count() - 1, -1, -1):
    4. x = self.gridLayout.itemAt(i).widget()
    5. x.setParent(self)
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: qgridlayout and hiding widgets

    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.

Similar Threads

  1. Replies: 2
    Last Post: 23rd October 2012, 01:10
  2. space between widgets in qgridlayout
    By demol in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 20:07
  3. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01
  4. Qt 3.3 QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2007, 17:40
  5. Performance in hiding/showing widgets
    By Paalrammer in forum Newbie
    Replies: 12
    Last Post: 14th February 2007, 18:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.