Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am building a QGIS plugin and would like to use a dock widget. I have used QT creator to design the UI, with the object type being a QDockWidget. When the plugin is enabled in QGIS the dockwidget appears to be inside of a normal dialog box. I am therefore unable to dock the plugin to the toolbar space etc. I have looked at other plugins using dock widgets but am unable to spot where I am going wrong. I used the plugin builder to generate my template code.

Result of running my plugin


from PyQt4 import QtCore, QtGui
from ui_Myplugin import Ui_MyPlugin

class MyPluginDialog(QtGui.QDockWidget, Ui_MyPlugin):
    def __init__(self):
        self.ui = Ui_MyPlugin()

part of myplugin.py

class myplugin:

def __init__(self, iface):
    # Save reference to the QGIS interface and map canvas
    self.iface = iface
    self.canvas = iface.mapCanvas()
    self.mapRenderer = self.canvas.mapRenderer()
    # initialize plugin directory
    self.plugin_dir = os.path.dirname(__file__)
    # initialize locale
    locale = QSettings().value("locale/userLocale")[0:2]
    localePath = os.path.join(self.plugin_dir, 'i18n', 'myplugin_{}.qm'.format(locale))

    if os.path.exists(localePath):
        self.translator = QTranslator()

        if qVersion() > '4.3.3':

    # Create the dialog (after translation) and keep reference
    self.dlg = mypluginDialog()

def initGui(self):
    # Create action that will start plugin configuration
    self.action = QAction(
        u"My Plugin", self.iface.mainWindow())
    # connect the action to the run method
    QObject.connect(self.dlg.ui.searchButton, SIGNAL("clicked()"), self.searchFromDialog)
    QObject.connect(self.dlg.ui.zoomButton, SIGNAL("clicked()"), self.zoom)

    self.toolbar = self.iface.addToolBar("My Plugin")
    self.toolbar.setObjectName("PMy Plugin")
    self.toolbarSearch = QLineEdit()
    self.toolbarSearch.setPlaceholderText("Type address and hit enter")

    # Add toolbar button and menu item
    self.iface.addPluginToMenu(u"&My Plugin", self.action)

1 Answer 1

up vote 2 down vote accepted

Have a look at this:


The relevant part being:

self.dock = MyPluginDialog()
self.iface.addDockWidget( Qt.RightDockWidgetArea, self.dock )
Thanks but I have compiled my UI file to python. I am now unsure how to reference my dock widget (rather than self.dock in the example link)? –  Matt Apr 3 at 9:35
... See update ... –  Matthias Kuhn Apr 3 at 9:37

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .