I'm pretty new to creating plugins, and python. I've created a plugin called open_projects that has 10 buttons, and I'd like each button to open up a project when its pressed. The plugin works when opened in QGIS, but I'm not sure how to apply the python logic to make the buttons open a project. So far I've tried this (unsuccessfully):

  1. Add QPushButton to the list of imports in open_projects.py:

    from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
    from PyQt4.QtGui import QAction, QIcon, QPushButton
  2. Add a new method:

    def PushButton1(self):
        from qgis.core import QgsProject
        from PyQt4.QtCore import QFileInfo
        project = QgsProject.instance()
        print project.filename

I know I'm probably way off!

2 Answers 2


Nice one! I think all you really needed to add was project.read(QFileInfo('c:/project1.qgs')) and then connect your button to this function using self.dockwidget.name_of_pushButton.clicked.connect(PushButton1).

Then repeat this for your 10 other buttons...which could be quite messy. Instead you could use the following which:

  • Create a dictionary assigning each pushbutton to a project.
  • Create a single function to load these projects depending on the pushbutton clicked.
  • Connect each pushbutton to this function.

Note: I'm assuming you used the Plugin Builder as @Zoltan mentioned and the plugin uses a dockwidget instead of dialog (otherwise you can replace dockwidget with dlg in the code).

This is what the main parts of the code looks like, include all imports at the beginning:

from qgis.core import QgsProject
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, Qt, QFileInfo
from PyQt4.QtGui import QAction, QIcon
import resources, qgis

Then in the run(self) function (PushButton1 is the object name of the first button etc):

def run(self):
    project_dict = {'PushButton1': 'project_1.qgs',
                    'PushButton2': 'project_2.qgs',
                    'PushButton3': 'project_3.qgs',
                    'PushButton4': 'project_4.qgs'}

    def load_project(button):
        project = project_dict[button.objectName()]
        QgsProject.instance().read(QFileInfo('path/to/projects/' + project))

    self.dockwidget.PushButton1.clicked.connect(lambda: load_project(self.dockwidget.PushButton1))
    self.dockwidget.PushButton2.clicked.connect(lambda: load_project(self.dockwidget.PushButton2))
    self.dockwidget.PushButton3.clicked.connect(lambda: load_project(self.dockwidget.PushButton3))
    self.dockwidget.PushButton4.clicked.connect(lambda: load_project(self.dockwidget.PushButton4))

I only did this for 4 pushbuttons but you can repeat it for 10.


Did you try plugin builder? It is a plugin which you can install from the menu Plugins/Manage and istall plugins. This plugin will create a workable plugin with a dialog box with two buttons (Cancel an OK). Then you should customize the dialog (*.ui file) with Qt Designer. You must install Qt designer, it is not part of the QGIS installer. Finally you should add event handler in your python code what to do when a button pushed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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