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.
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)
QgsDockWidget
. I guess it didn't exist back then and it is now the recommended widget class for this use case.