I wonder if it's possible to add an plugin button (QAction) to the print composer toolbar. I only know options to add the plugin-buttons to the plugin,vector or database menu or to own toolbars in the main window of QGIS.

  • 2
    Nice to see you're making progress! If you come up with a possible solution, remember to post it as an answer and not as an edit to your question :) – Joseph Nov 11 '16 at 13:58

I found a way to solve the problem. I am just not sure if self.iface.mainWindow() is the correct parent QObject for this action.

import qgis
from PyQt4.QtCore import *
from PyQt4.QtGui import *

self.activeComposer = self.iface.activeComposers()

for item in self.activeComposer:
    print item.composerWindow().windowTitle()
    if item.composerWindow().windowTitle()=='test123':
        print "ja"
        self.composerWindow = item.composerWindow()
        print self.composerWindow
        self.action = QAction(u"Druckstempel einfügen", self.iface.mainWindow())
        # is self.iface.mainWindow() the correct QObject * parent for this Action?
        self.action.setWhatsThis(u"Druckstempel einfügen")
        self.action.setStatusTip(u"Druckstempel einfügen")


enter image description here

  • how did you finally added the button in the print layout ? – Ankit Chaudhri Apr 22 '20 at 11:15

For Python 3, you can get the reference to the left toolbar with

# Get all designers opened
# Limited because references lost when layouts are closed

# Add a button to redirect to webpage, borrowed from
# https://courses.spatialthoughts.com/pyqgis-in-a-day.html#add-a-new-menu-item
import webbrowser

def open_website():

website_action = QAction('Go to gis.stackexchange')
## End of borrowed section

def getRefToToolbar(layoutDesigner):
    # Access to left bar
    toolbox = layoutDesigner.window().findChild(QToolBar, 'mToolsToolbar')
    # Add PDF printing in the toolbar
    pdfAction = iface.openLayoutDesigners()[0].window().findChild(QAction, 'mActionExportReportAsPDF')
    # Methods to access to most menus and toolbars are listed at
    # https://qgis.org/api/classQgsLayoutDesignerInterface.html#acd6e5ffa28aece32fbeda73c68b2a79f

# After executing this line, open a window to see changes (the button to print PDF is now in the left toolbar)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.