I'm creating an expandable/collapsible widget because Qt doesn't provide such feature until now. My problem is, that the button to expand/collapse the widget seems to be only clickable, if you make the window very large (only the width of the window is relevant). The elements of the the main widget are working as intended. Does anyone know the reason for it?
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QLabel, QMainWindow, QVBoxLayout, QHBoxLayout, QToolButton
from PyQt5.QtCore import Qt
class ExpandableWidget(QWidget):
'''A widget that can be expanded / collapsed by the user'''
def __init__(self):
super(ExpandableWidget, self).__init__()
self.titleWidget = QWidget() # widget containing the title bar (always visible)
self.mainWidget = QWidget() # widget containing the collapsible content (sometimes visible, but this isn't implemented yet)
self.expandButton = QToolButton()
self.expandButton.setCheckable(True)
self.expandButton.setChecked(False)
self.expandButton.setArrowType(Qt.LeftArrow)
self.titleLayout = QHBoxLayout()
self.titleLayout.addWidget(self.titleWidget)
self.titleLayout.addWidget(self.expandButton)
self.wrapperLayout = QVBoxLayout()
self.wrapperLayout.addLayout(self.titleLayout)
self.wrapperLayout.addWidget(self.mainWidget)
self.setLayout(self.wrapperLayout)
def setMainWidget(self, widget):
self.wrapperLayout.replaceWidget(self.mainWidget, widget)
self.mainWidget = widget
def setTitleWidget(self, widget):
self.titleLayout.replaceWidget(self.titleWidget, widget)
self.titleWidget = widget
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = QMainWindow()
expandable = ExpandableWidget()
mainWindow.setCentralWidget(expandable)
mainWidget = QPushButton("Button")
titleLabel = QLabel("Title Label")
expandable.setTitleWidget(titleLabel)
expandable.setMainWidget(mainWidget)
mainWindow.show()
sys.exit(app.exec_())
GIF demonstrating the effect