Qt - Pyside - .saveGeom() .saveState() (again)

2019-08-13 18:04发布

问题:

This is a follow on question to Qt - pyside - saveGeometry() saveState()

I have a Qt program and currently I use Qsettings and the mainWindow.saveGeometry() and mainWindow.saveState() functions to allow the program to restore the layout that the user set in the previous session.

This approach works well for the docked position of all the docked windows. It also works well for the floating position of the un-docked windows provided the windows are un-docked at exit.

When a user tailors the position of the floating window to their needs within a session, docking and re-floating the window cause it to successfully restore the users preferred position.

However the problem arises when the user has tailored the size and position of the window and then docks the window and then exits. On restart the window does not recover the users preferred floating position.

None of the suggestions in the answers to the other question linked above are helpful since QdockWidget does not have any getGeom() setGeom() type methods. Neither does it have its own seveGeometry() restoreGeometry() methods. I guess the main window methods are supposed to take care of dockedwidgets too. But the above behaviour falls short of the desired functionality.

I have tried using save and restore methods on the object returned by the widget() method of QdockWidget but that does not work. (It modifies the contents of the QdockWidget and not the position of the floating window)

So I think my refined questions are these:
1) Have others observed the above shortcoming with main window save/restore or might I be doing something to block proper restoring of the docked windows.
2) Is there any way to get and set the floating geometry of the QdockWidget?

回答1:

Ok guys. Here is the answer. There is a bug in Qt. When the main window is maximised and the QdocWidget's are docked (not floating) then the floating position is not saved.

This code is a simple workaround.

to save:

settings = QtCore.QSettings(org_name, app_name)
is_floating = main_win._ui.dockWin.isFloating()
settings.setValue('dockWin/isFloating', is_floating)
main_win._ui.dockWin.setFloating(True)
settings.setValue('geometry', main_win.saveGeometry())
settings.setValue('state', main_win.saveState())

to restore:

settings = QtCore.QSettings(org_name, app_name)
main_win.restoreGeometry(settings.value('geometry'))
main_win.restoreState(settings.value('state'))
main_win._ui.dockWin.setFloating(settings.value('dockWin/isFloating')=='true')