I need to create a gui with collapsible button consisting of few push buttons. Is there any way to add collapsible widget to the main layout in PyQt5? There is no proper documentation available for this. I have created the tree widget but it is showing as a scrollable button as shown in second pic. Attached the expected result and code below.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,
QAction,QMainWindow,QDialog,QTreeWidget, QTreeWidgetItem, QLabel, QVBoxLayout, QLineEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtCore, QtGui, QtWidgets
from dld import TreeWidgetWithWidgetItems
class App(QMainWindow,QWidget):
def __init__(self):
super().__init__()
self.title = 'Status bar'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar().showMessage('Message in statusbar.')
mainMenu=self.menuBar()
fileMenu=mainMenu.addMenu('File')
confMenu = mainMenu.addMenu('Configure')
aboutMenu = mainMenu.addMenu('About')
exitButton = QAction('Exit', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(10, 60)
button.clicked.connect(self.on_click)
# Creating top level and child widgets
tLB = QPushButton("Top Level Button",self)
# tLB.move(10,130)
cB1 = QPushButton("Child 1",self)
# cB1.move(40,60)
cB2 = QPushButton("Child 2",self)
# cB2.move(30,100)
cB3 = QPushButton("Child 3",self)
# cB3.move(40,150)
childLineEdit = QLineEdit()
childItems=[]
treeWidget = QTreeWidget(self)
treeWidget.move(10, 150)
treeWidget.setHeaderLabel("TreeWidget with Buttons")
topLevelItem = QTreeWidgetItem()
for i in range(4):
childItems.append(QTreeWidgetItem())
topLevelItem.addChild(childItems[i])
treeWidget.addTopLevelItem(topLevelItem)
treeWidget.setItemWidget(topLevelItem, 0, tLB)
# Replacing the child items with widgets
treeWidget.setItemWidget(childItems[0], 0, cB1)
treeWidget.setItemWidget(childItems[1], 0, cB2)
treeWidget.setItemWidget(childItems[2], 0, cB3)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ex = App()
sys.exit(app.exec_())