6

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

myplugindialog.py

from PyQt4 import QtCore, QtGui
from ui_Myplugin import Ui_MyPlugin
class MyPluginDialog(QtGui.QDockWidget, Ui_MyPlugin):
 def __init__(self):
 QtGui.QDockWidget.__init__(self)
 self.ui = Ui_MyPlugin()
 self.ui.setupUi(self)

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()
 self.translator.load(localePath)
 if qVersion() > '4.3.3':
 QCoreApplication.installTranslator(self.translator)
 # Create the dialog (after translation) and keep reference
 self.dlg = mypluginDialog()
def initGui(self):
 # Create action that will start plugin configuration
 self.action = QAction(
 QIcon(":/plugins/myplugin/icon.png"),
 u"My Plugin", self.iface.mainWindow())
 # connect the action to the run method
 self.action.triggered.connect(self.run)
 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.toolbar.addAction(self.action)
 self.toolbarSearch = QLineEdit()
 self.toolbarSearch.setMaximumWidth(200)
 self.toolbarSearch.setAlignment(Qt.AlignLeft)
 self.toolbarSearch.setPlaceholderText("Type address and hit enter")
 self.toolbar.addWidget(self.toolbarSearch)
 self.toolbarSearch.returnPressed.connect(self.searchAddressFromToolbar)
 # Add toolbar button and menu item
 self.iface.addPluginToMenu(u"&My Plugin", self.action)
Val P
3,9501 gold badge10 silver badges34 bronze badges
asked Apr 3, 2014 at 9:17
1
  • 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. Commented Apr 13, 2018 at 12:14

1 Answer 1

11

Have a look at this:

http://anitagraser.com/2010/11/08/adding-a-dock-widget-to-qgis/

The relevant part being:

self.dock = MyPluginDialog()
self.iface.addDockWidget( Qt.RightDockWidgetArea, self.dock )
answered Apr 3, 2014 at 9:20
2
  • 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)? Commented Apr 3, 2014 at 9:35
  • 4
    ... See update ... Commented Apr 3, 2014 at 9:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.