0

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_())

expected output with menubar included as in second image

output

Bharath
  • 113
  • 1
  • 1
  • 10
  • If you don't need the features of the tree view, a possible implementation is here: [How to create collapsible box in PyQt](https://stackoverflow.com/questions/52615115/how-to-create-collapsible-box-in-pyqt). – musicamante Feb 15 '20 at 16:43
  • I'm able create the collapsible menu as shown in second pic. But when i add it to main window it is shown as a scrollbar as in pic 1 – Bharath Feb 15 '20 at 18:42
  • So you *do* need to use a QTreeView or not? If you don't, follow the example linked in my comment. In any case: 1. if you don't set a widget as a central in QMainWindow, it'll never be resized to the window size: use `setCentralWidget(treeWidget)` so that it'll be added to the main window *internal* layout, or create a container QWidget with a layout, add the treewidget to it and then set that QWidget as central widget of the mainwindow. 2. QMainWindow already inherits from QWidget, remove QWidget from the constructor. 3. Don't overwrite base QWidget attributes like `width` or `height`. – musicamante Feb 15 '20 at 18:58

0 Answers0