I have a simple UI with QWidgets within a QTreeWidget.
Some of them need to be hidden.
Every time the application is resized, all QWidgets become visible.
Below is a minimal example to demonstrate the issue.
How can I make sure the QWidgets stay hidden until setVisible(True)
is called explicitly?
pyqt version is 5.12.3
from PyQt5.QtWidgets import QApplication, QWidget, QTreeWidgetItem, QTreeWidget, QCheckBox, QPushButton, QVBoxLayout
import sys
class Foo(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout(self)
tree = QTreeWidget()
item = QTreeWidgetItem()
tree.addTopLevelItem(item)
self.checkBox = QCheckBox()
tree.setItemWidget(item,0,self.checkBox)
button = QPushButton('Hide Checkbox')
button.clicked.connect(self.hideCheckbox)
lay.addWidget(tree)
lay.addWidget(button)
lay.addWidget(button)
def hideCheckbox(self):
self.checkBox.setVisible(False) # is reset on resize
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Foo()
w.show()
sys.exit(app.exec_())