<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>Hi,<br><br></div>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.<br>
<br></div>The example is at: <a href="http://paste.ubuntu.com/7201238/">http://paste.ubuntu.com/7201238/</a><br><br></div>The docs for QMainWindow>>restoreState say:<br><br>"""<br>Restores the <i>state</i> of this mainwindow's toolbars and dockwidgets. The <i>version</i> number is compared with that stored in <i>state</i>. If they do not match, the mainwindow's state is left unchanged, and this function returns <tt>false</tt>; otherwise, the state is restored, and this function returns <tt>true</tt>.<br>
"""<br><br></div>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. <br>
<br>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.<br><br>I have double checked the following:<br>
<br></div>1) My dock widget is created *before* restoreState() is called.<br></div>2) I've called 'setObjectName' on my dock widget, and it's given a unique name.<br></div>3) Qsettings is creating a data file (under Linux it's ~/.config/TestCompany/TestApp.conf), and it contains data.<br>
<br></div>The example is simple enough that I feel like I must be missing something really obvious.<br><br>In my experimentation, I've found that if I add a call to 'restoreDockWidget' in my minimal example like this:<br>
<br></div>--<code>--<br>def readSettings(self):<br> settings = QtCore.QSettings()<br> if six.PY3:<br> self.restoreGeometry(settings.value("geometry").data())<br> self.restoreState(settings.value("windowState").data())<br>
else:<br> self.restoreGeometry(settings.value("geometry").toByteArray())<br> self.restoreState(settings.value("windowState").toByteArray())<br> self.restoreDockWidget(self.filter_widget)<br>
# make the toggle button in the same state as the dock widget.<br> self.toggle_dock_widget_action.setChecked(<br> self.dockWidgetArea(self.filter_widget) !=<br> QtCore.Qt.NoDockWidgetArea<br>
)<br></div>--</code>--<br><br></div>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:<br>
<br></div><div>--<code>--<br>if not self.restoreDockWidget(self.filter_widget):<br> self.addDockWidget(<br> QtCore.Qt.RightDockWidgetArea,<br> self.filter_widget,<br> )<br>
</div><div>--</code>--<br><br></div><div>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?<br>
<br></div><div>I'm using version 4.10.4+dfsg-1 as packaged in Ubuntu Trusty Tahr.<br><br></div><div>Thanks in advance for your time.<br><br>Cheers!<br></div><div>-- <br>Thomi Richards<br>
</div></div>