3

I'm developing a custom QGIS application using the QGIS API. I've been using the API provided with QGIS Brighton (Version 2.6.1) and have been developing the application in Python in a Windows 7 environment. I have provided a basic map canvas for my application with an accompanying layer tree view. While most of the functionality appears to be working fine, I've been unable to get a pop-up menu to display when right-clicking on a layer in the layer tree view. I have followed the scheme suggested by Martin Dobias in his "QGIS Layer Tree API (Part 3)" post on the Jan 30 2015. I've also looked through the qgisapp.cpp source code, and although written in C++, my Python application seems to be following the same approach as the qgis code. I have included contextMenuEvent event handler with is called when I right click on the layer tree. However the createContextMenu() function in the Menu Provider is not being called. Do I need to explicitly call it from contextMenuEvent event handler. This doesn't appear to be what is required from Martin's post.

I've included a couple of code snippets so that you can see what I am doing.

class FeweMenuProvider(QgsLayerTreeViewMenuProvider):

    def __init__(self, view):
        QgsLayerTreeViewMenuProvider.__init__(self)
        print "FeweMenuProvider.init()"
        self.menuProviderView = view

    def createContextMenu(self):
        print "FeweMenuProvider.createContextMenu()"
        if not self.menuProviderView.currentLayer():

            print "No Layer - Got in here at least"
            return None

        contextMenu = QMenu()
        contextMenu.addAction("Show Extent", self.showExtent)
        print "Layer clicked."
        return contextMenu

    def showExtent(self):
        print "FeweMenuProvider.showExtent()"
        r = self.menuProviderView.currentLayer().extent()
        QMessageBox.information(None, "Extent", r.toString())


class FeweMainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(FeweMainWindow, self).__init__(parent)
.
.
.

            self.rootNode = QgsProject.instance().layerTreeRoot()
            self.model = QgsLayerTreeModel(self.rootNode, self)
            self.model.setFlag(QgsLayerTreeModel.AllowNodeReorder, True)
            self.model.setFlag(QgsLayerTreeModel.AllowNodeRename, True)
            self.model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility, True)
            self.model.setAutoCollapseLegendNodes(10)

            self.feweLayerTreeView = QgsLayerTreeView(self.centralWidget)
            self.feweLayerTreeView.setGeometry(10, 40, 171, 521)
            self.feweLayerTreeView.setModel(self.model)

            # Create popup menu for layer tree
            #
            contextMenuProvider = FeweMenuProvider(self.feweLayerTreeView)
            self.feweLayerTreeView.setMenuProvider(contextMenuProvider)

.
.
.
    def contextMenuEvent(self, event):

        print "Got into contextMenuEvent()"
        QMainWindow.contextMenuEvent(self, event)   
        #menu = self.feweLayerTreeView.menuProvider().createContextMenu()
        #print menu
        #if menu is not None:
        #    print "About to show menu."
        #    menu.show()
        #event.accept()
  • I just recently tried this same approach, and it failed. What's more is that the mere declaration of menu provider class made the entire layertreeview disappear... is this class out-dated or are we supposed to use another approach? – For Comment Apr 11 '17 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.