How to hide a layout in PyQt?

后端 未结 2 1663
臣服心动
臣服心动 2020-12-10 19:45

My code contains a vertical box layout which is a combination of a vertical box layout in left and one at right. I was wondering if there is a way to hide the left layout wi

相关标签:
2条回答
  • 2020-12-10 20:25

    You can't hide a layout, but you can hide a widget.

    So first put all the widgets in a container widget. Then connect your signal to the setHidden() slot of the container widget. Your signal should emit True or False, depending on whether you want to hide or show the widgets. Alternatively, you could connect your signal to a simple toggle slot, like this:

        def toggleLeftWidget(self):
            self.leftWidget.setHidden(not self.leftWidget.isHidden())
    

    In which case, it wouldn't matter what your signal emitted.

    0 讨论(0)
  • 2020-12-10 20:36

    You could cheat and use a frame instead of a layout: It works exactly the same way, except for the fact you have to set a layout on the frame for it to work properly. You can then do the following:

    from PyQt5 import QtWidgets
    
    # create the frame object.
    frame = QtWidgets.QFrame()
    
    # you can do this with any layout - vbox, grid, hbox... 
    # There will not be more than one item in it anyway.
    ly = QtWidgets.QVBoxLayout()
    frame.setLayout(ly)
    
    # we're assuming here that parent_layout is some outside layout object.
    parent_layout.addWidget(frame)
    
    # hide the frame and its contents
    frame.hide()
    # show the frame and its contents
    frame.show()
    

    I was looking for a solution like this, hope this helps :)

    0 讨论(0)
提交回复
热议问题