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 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)
  • I just installed Qt 5.7.1 and Qt 3, and Qt Designer has a QGIS custom widgets section with a QgsDockWidget. I guess it didn't exist back then and it is now the recommended widget class for this use case.
    – Jérôme
    Apr 13, 2018 at 12:14

1 Answer 1


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, 2014 at 9:35
  • 4
    ... See update ... Apr 3, 2014 at 9:37

Your Answer

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

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