I'm playing with QDockWidgets. Here is the piece of code i'm trying :

def dock(self):
    widget = uic.loadUi("C:\dock.ui")
    self.iface.addDockWidget(Qt.RightDockWidgetArea, widget)
    widget.setWindowTitle("Dock ...")
    #QMessageBox.information(None,"Testing dockWidgets ...","Working or not ... ?")

When the QmessageBox line is left commented nothing works (the widget doesn't appear on the screen nor in the panel list of QGIS)

When the Qmessagebox line is uncommented the widget appears, title is set but widget disappear once the QMessageBox is closed ...

I fear i might be something linked with the indentation of my code but i feel i tried everything ...

Am I forgetting something ?

What should i do to have the widget remain after this piece of code is executed ?

Or could it be linked with the dynamic loading of the widget's ui ?


widget is going out of scope. Pretty sure addDockWidget doesn't take ownership of the object so you will need to keep widget alive as long as you need it or else Python will delete once the method ends.

self.widget =will be a start here. You should also check to see if self.widget is None first and only do the loadUi call then.

  • self.widget wasn't None but adding self solved the problem (of course). I shouldn't code sleepy ... Thanks – snaileater Mar 27 '18 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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