[PyQt] QMainWindow.restoreState does not restore docked widget

Thomi Richards thomir at gmail.com
Fri Apr 4 02:05:42 BST 2014


Hi,

I'm having difficulties making my main window restore my dock widget
correctly. I've produced a minimal complete example to demonstrate my
problem. The minimal example is still 73 lines long, which is perhaps a
little long to include in an email in full. I'll post a link to it, and
then quote snippets thereafter.

The example is at: http://paste.ubuntu.com/7201238/

The docs for QMainWindow>>restoreState say:

"""
Restores the *state* of this mainwindow's toolbars and dockwidgets. The
*version* number is compared with that stored in *state*. If they do not
match, the mainwindow's state is left unchanged, and this function returns
false; otherwise, the state is restored, and this function returns true.
"""

The first time I run the minimal example, the dock widget isn't shown, but
I guess that's to be expected - QSettings won't have returned any data,
since the config file doesn't exist yet.

However, if I show the dock widget (by clicking on the toolbar button) and
then close the window, the next time I run the minimal example I expect the
dock widget to be shown, but it's not.

I have double checked the following:

1) My dock widget is created *before* restoreState() is called.
2) I've called 'setObjectName' on my dock widget, and it's given a unique
name.
3) Qsettings is creating a data file (under Linux it's
~/.config/TestCompany/TestApp.conf), and it contains data.

The example is simple enough that I feel like I must be missing something
really obvious.

In my experimentation, I've found that if I add a call to
'restoreDockWidget' in my minimal example like this:

--<code>--
def readSettings(self):
        settings = QtCore.QSettings()
        if six.PY3:
            self.restoreGeometry(settings.value("geometry").data())
            self.restoreState(settings.value("windowState").data())
        else:
            self.restoreGeometry(settings.value("geometry").toByteArray())
            self.restoreState(settings.value("windowState").toByteArray())
        self.restoreDockWidget(self.filter_widget)
        # make the toggle button in the same state as the dock widget.
        self.toggle_dock_widget_action.setChecked(
            self.dockWidgetArea(self.filter_widget) !=
            QtCore.Qt.NoDockWidgetArea
        )
--</code>--

Then the dock widget is restored from the QSettings data, but the first
time the app is run, the dock widget is shown, even though there's no
Qsettings data. I expected 'restoreDockWidget' to return False in this case
(and not show the dock widget), but it seems to return True every time,
which means I cannot detect that case and handle it like so:

--<code>--
if not self.restoreDockWidget(self.filter_widget):
            self.addDockWidget(
                QtCore.Qt.RightDockWidgetArea,
                self.filter_widget,
            )
--</code>--

Sorry for the long email, but I'm hoping someone can point me in the
correct direction here. What's the recommended way of restoring a dock
widget from QSettings data, and also for providing a sensible default
position the first time the application is run?

I'm using version 4.10.4+dfsg-1 as packaged in Ubuntu Trusty Tahr.

Thanks in advance for your time.

Cheers!
-- 
Thomi Richards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140404/7f661d69/attachment.html>


More information about the PyQt mailing list