Python Forum

Full Version: Help: Dockable widget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a small GUI that has multiple group boxes (see below code).
Group box 1 (Named: Example 1) can be made visible or hidden using the menu.
However, instead of having the user to select whether this group box is visible or not, I would like to make it dockable to the left side of the GUI.
I have read some instructions about dock widgets but, can't get grip of how to add it to my code.

Can somebody give me some advise?

import sys, os
from PySide2.QtWidgets import QApplication, QDialog, QAction, QMenuBar, QGroupBox, QActionGroup, QHBoxLayout, \
    QVBoxLayout, QStyleFactory


class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setFixedHeight(400)
        self.menu_bar = QMenuBar()
        self.make_menu_bar()
        self.layout = QHBoxLayout()
        self.layout.setSpacing(1)
        self.layout.setMenuBar(self.menu_bar)
        self.group1 = QGroupBox('Example1')
        self.group2 = QGroupBox('Example2')
        self.group3 = QGroupBox('Example3')
        self.group4 = QGroupBox('Example4')
        self.group5 = QGroupBox('Example5')

        self.set_layout()
        self.setLayout(self.layout)

    def set_layout(self):

        vbox = QVBoxLayout()
        vbox.addWidget(self.group2)
        vbox.addWidget(self.group3)
        self.layout.setSpacing(1)
        self.layout.addWidget(self.group1)
        self.layout.addLayout(vbox)
        self.layout.addWidget(self.group4)
        self.layout.addWidget(self.group5)

        self.group1.setFixedWidth(50)
        self.group2.setFixedWidth(100)
        self.group3.setFixedWidth(100)
        self.group4.setFixedWidth(300)
        self.group5.setFixedWidth(200)

    def make_menu_bar(self):
        display = self.menu_bar.addMenu('Display content')
        treeview = display.addMenu('Treeview')
        treeview_ag = QActionGroup(display, exclusive=True)
        treeview .addAction(treeview_ag.addAction(QAction('Hidden', self.menu_bar, checkable=True)))
        treeview .addAction(treeview_ag.addAction(QAction('Visible', self.menu_bar, checkable=True)))
        treeview_ag.triggered[QAction].connect(self.view_tree_view)

    def view_tree_view(self, action):
        text = action.text()
        if text == 'Visible':
            self.group1.setVisible(True)
        else:
            self.group1.setVisible(False)



if __name__ == '__main__':
    os.getcwd()
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    form = Form()
    form.show()
    sys.exit(app.exec_())