[QGIS-Developer] How to restore dock widget plugin size after relaunch of QGIS?

Jean-Baptiste Peter jbpeter at outlook.com
Thu Nov 17 09:41:45 PST 2022


Thanks, adding .setObjectName() in the constructor of the dock widget subclass worked to restore the dimension of the widget. However, the dock widget does not show automatically at QGIS startup even if it was open during the last session. What is the proper way to achieve this?

 Here is the code that I'm using to initialize and run the dock widget:


    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""
        
        self.log("init")
        
        icon_path = ':/plugins/layout_panel/icon.png'
        self.add_action(
            icon_path,
            text=self.tr(u'Layout Panel'),
            callback=self.run,
            parent=self.iface.mainWindow())
                
        if self.dockwidget == None:
            self.dockwidget = LayoutPanelDockWidget(self.iface, self.iface.mainWindow())
            
        self.dockwidget.closingPlugin.connect(self.onClosePlugin)
        self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockwidget)
              

    def onClosePlugin(self):
        """Cleanup necessary items here when plugin dockwidget is closed"""
        self.pluginIsActive = False


    def unload(self):
        """Removes the plugin menu item and icon from QGIS GUI."""

        for action in self.actions:
            self.iface.removePluginMenu(
                self.tr(u'&Layout Panel'),
                action)
            self.iface.removeToolBarIcon(action)
        # remove the toolbar
        del self.toolbar


    def run(self):
        """Run method that loads and starts the plugin"""
        if not self.pluginIsActive:
            self.pluginIsActive = True
            self.dockwidget.show()


Thank you!

Best,

--
Jean-Baptiste PETER

-----Original Message-----
From: Nyall Dawson <nyall.dawson at gmail.com> 
Sent: Wednesday, November 16, 2022 11:31 PM
To: Jean-Baptiste Peter <jbpeter at outlook.com>
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [QGIS-Developer] How to restore dock widget plugin size after relaunch of QGIS?

On Thu, 17 Nov 2022 at 02:22, Jean-Baptiste Peter via QGIS-Developer <qgis-developer at lists.osgeo.org> wrote:
>
> Hi,
>
>
>
> I am working on a QGIS plugin which is based on QGIS plugin Builder dock widget template. The default behavior of this template is that the dock widget is not restored after closing and reopening QGIS. Here is the repo of my plugin:
>
> https://github.com/jbp35/layout_panel

Try calling .setObjectName() in the constructor of your dock widget subclass. That's required for the standard QGIS dock arrangement save/restore code to work correctly. (That's all that's needed -- you don't need to manually do any calls to saveGeometry/restoreGeometry).

Also I suggest using the QgsDockWidget subclass as the base class for your widget, as it has some extra niceness added on top of the standard Qt QDockWidget class.

Nyall


>
>
>
> I managed to reopen the dock widget automatically when QGIS starts by calling the run() function from initGui() function. However, the dimensions of the widget is not saved and I am not able to figure out how to restore the width and height of the widget. I tried to use QDockWidget::restoreGeometry without success. Could someone please point me how to achieve this? Thanks!
>
>
>
> Best,
>
>
>
> --
>
> Jean-Baptiste PETER
>
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list