PyQt5 QDockWidget Title Bar Widgets

164 Views Asked by At

I am creating a plugin for QGIS using PyQt5 and have implemented a dock widget that shows all layers. I was wondering if it was possible to add buttons to the Title Bar of the dock widget like the ones QGIS has QGIS Dock Widget Title Bar Widgets.

I looking into the documentation and I know there is a setTitleBarWidget but that sets only one widget. The other thing I tried was created a QToolBar as a child of the QDockWidget which seemed to work but not completely. Floating and Docked. I also tried setContentsMargins(0, 30, 0, 0) but that didn't work either. Playing with other values only increased/decreased the other sides but not the title margin. Any help or guidance is greatly appreciated. Thank you.

def layerTree(self):
        self.layers_widget = QDockWidget('Layers', self)


        self.view = QgsLayerTreeView(self.layers_widget)
        self.root = QgsProject.instance().layerTreeRoot()
        self.model = QgsLayerTreeModel(self.root)
        self.model.setFlag(QgsLayerTreeModel.AllowNodeReorder)
        self.model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility)
        self.view.setModel(self.model)
        self.layers_widget.setWidget(self.view)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.layers_widget)


        self.lt_toolbar = QToolBar(self.layers_widget)
        self.delete_layer = QAction('Delete Layer')
        self.lt_toolbar.addAction(self.delete_layer)
1

There are 1 best solutions below

2
nospec On

musicamante, thank you! that's exactly what I did and came here to share it. The answer follows what you said in your comment

def layerTree(self):
    self.layers_widget = QDockWidget('Layers', self)
    self.view = QgsLayerTreeView(self.layers_widget)
    self.root = QgsProject.instance().layerTreeRoot()
    self.model = QgsLayerTreeModel(self.root)
    self.model.setFlag(QgsLayerTreeModel.AllowNodeReorder)
    self.model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility)
    self.view.setModel(self.model)
    
    self.addDockWidget(Qt.LeftDockWidgetArea, self.layers_widget)

    self.dock_layout_widget = QWidget()
    self.dock_layout = QBoxLayout(QBoxLayout.TopToBottom, self.dock_layout_widget)
    self.dock_layout.setContentsMargins(0,0,0,0)

    self.dock_toolbar = QToolBar()
    newLetter = QAction("New", self)
    self.dock_toolbar.addAction(newLetter)
    self.dock_layout.addWidget(self.dock_toolbar)
    self.dock_layout.addWidget(self.view)

    self.layers_widget.setWidget(self.dock_layout_widget)