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.
-
2Nice 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.setIcon(QIcon(":/icons/cursor.png"))
self.action.setWhatsThis(u"Druckstempel einfügen")
self.action.setStatusTip(u"Druckstempel einfügen")
self.test_toolbar=self.composerWindow.addToolBar("Test")
self.test_toolbar.addAction(self.action)
self.action.triggered.connect(self.run)
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
iface.openLayoutDesigners()
# 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():
webbrowser.open('https://gis.stackexchange.com')
website_action = QAction('Go to gis.stackexchange')
website_action.triggered.connect(open_website)
## End of borrowed section
def getRefToToolbar(layoutDesigner):
# Access to left bar
toolbox = layoutDesigner.window().findChild(QToolBar, 'mToolsToolbar')
print(toolbox)
# Add PDF printing in the toolbar
pdfAction = iface.openLayoutDesigners()[0].window().findChild(QAction, 'mActionExportReportAsPDF')
toolbox.addAction(pdfAction)
# Methods to access to most menus and toolbars are listed at
# https://qgis.org/api/classQgsLayoutDesignerInterface.html#acd6e5ffa28aece32fbeda73c68b2a79f
layoutDesigner.layoutMenu().addAction(website_action)
# After executing this line, open a window to see changes (the button to print PDF is now in the left toolbar)
iface.layoutDesignerOpened.connect(getRefToToolbar)