1

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)
nospec
  • 41
  • 5
  • Just create a basic QWidget, set a layout for it, add any widget you want to that layout and finally call `setWidget()` on that QWidget. – musicamante Apr 24 '23 at 22:44

1 Answers1

3

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)
nospec
  • 41
  • 5
  • Glad you found the solution on your own. Some notes, though, if I may: 1. There's no need to use QBoxLayout with `TopToBottom`, just use `QVBoxLayout(self.dock_layout_widget)` (AFAIK, there are no *common* known UI layouts that are vertically set from bottom to top); 2. Remember that StackOverflow is *not* a forum, I appreciate your recognition, but that text is not very useful for the answer: instead, expand your answer by explaining what you did and, if you still want to "credit" someone, add it as a link to the comment (right click on the comment time stamp) or the user URL. – musicamante Apr 25 '23 at 01:39
  • @musicamante thank you for your guidance. I have edited the answer. Also, I'll make the changes you suggested in my code. – nospec Apr 25 '23 at 02:04