Results 1 to 8 of 8

Thread: Show/Hide height animation with SetFixedSize

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Show/Hide height animation with SetFixedSize

    I've been trying for quite a while now to have a window which automatically shrinks to the size of its widgets, and whose child widgets' hide/show operations will be animated.

    So far I've managed to get a window which will shrink, using window.layout().setSizeConstraint(QLayout.SetFixed Size) - and then overriding the widget's sizeHint (see the code).

    I have been experimenting with different sizePolicies on the widget and different sizeConstraints on the encapsulating layouts -- needless to say, they have each had slightly different (and horrible) results. Here is my code -- in which the animation doesn't work at all:

    Qt Code:
    1. #!/usr/bin/env python
    2.  
    3. from PyQt4.QtGui import *
    4. from PyQt4.QtCore import *
    5. import sys
    6.  
    7. connect = QObject.connect
    8.  
    9. app = QApplication(sys.argv)
    10.  
    11. def print_qsize(sz, title=""):
    12. print title, "W: ", sz.width(), " H: ", sz.height()
    13.  
    14. class ResizingWindow(QMainWindow):
    15. def __init__(self, parent=None):
    16. QMainWindow.__init__(self, parent)
    17. centralwidget = QWidget(self)
    18. self.setCentralWidget(centralwidget)
    19. layout = QVBoxLayout()
    20. centralwidget.setLayout(layout)
    21.  
    22. checkbox1 = QCheckBox("Show First Widget", self)
    23. checkbox2 = QCheckBox("Show Second Widget", self)
    24. textedit1 = QPlainTextEdit(self)
    25. textedit2 = QPlainTextEdit(self)
    26.  
    27. layout.addWidget(checkbox1)
    28. layout.addWidget(textedit1)
    29. layout.addWidget(checkbox2)
    30. layout.addWidget(textedit2)
    31.  
    32. connect(checkbox1, SIGNAL("toggled(bool)"), lambda b: self.hide_resize(textedit1, b))
    33. connect(checkbox2, SIGNAL("toggled(bool)"), lambda b: self.hide_resize(textedit2, b))
    34.  
    35. self.modify_widget(textedit1)
    36. self.modify_widget(textedit2)
    37. textedit1.hide()
    38. textedit2.hide()
    39.  
    40. self.layout().setSizeConstraint(QLayout.SetFixedSize)
    41.  
    42. def modify_widget(self, widget):
    43. #this is needed so that the widget 'inherits' the existing size of the
    44. #layout rather than forcing the layout to expand even further.. don't ask
    45. #me how this works
    46. def sizeHint():
    47. return QSize(1, 1)
    48. widget.sizeHint = sizeHint
    49.  
    50. def hide_resize(self, widget, show=True):
    51. if show:
    52. #this doesn't work
    53. widget.resize(widget.width(), 1)
    54. widget.show()
    55.  
    56. #neither does this
    57. widget.resize(widget.width(), 1)
    58.  
    59. #and neither do these:
    60. a = QPropertyAnimation(widget, "height", widget)
    61. a.setStartValue(1)
    62. a.setEndValue(100)
    63. a.setDuration(250)
    64. a.start()
    65. else:
    66. widget.hide()
    67.  
    68. mw = ResizingWindow()
    69. mw.show()
    70. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    My question is how to make the animation here work? I'm quite sure it involves a magic combination of sizeConstraints and sizePolicy settings, and maybe even a wrapping container widget -- but I haven't found the right combination yet.

    Update: Trying to animate the size property rather than just the height yields some results, however I prefer to only dabble with the height, as I would like to leave the width managed by other things - it would be a pain to go through all the layouts and their margins, and then set that width manually by calculating the size of the top-most widget, then subtracting the margins of the layouts

    Also, it seems that the animation only works on the widget, but not the window itself (which would be really nice) -- so e.g. the window first expands the full (allowed?) size, and then the widget animates (improperly though)
    Last edited by mnunberg; 20th September 2010 at 00:00. Reason: spelling corrections

Similar Threads

  1. how to show an app if it manually hide
    By jthacker in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2010, 13:02
  2. how to show and hide frames?
    By rambo83 in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 09:53
  3. Hide and Show QMenu
    By febil in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2009, 09:31
  4. hide from taskbar but show to ALT-TAB
    By musikit in forum Qt Programming
    Replies: 0
    Last Post: 15th August 2008, 16:14
  5. Show or hide a form
    By Gayathri in forum Newbie
    Replies: 11
    Last Post: 17th November 2006, 12:39

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
  •  
Qt is a trademark of The Qt Company.