I have a Qt program and currently I use the Qsettings.saveGeometry() and Qsettings.saveState() functions to allow the program to restore the layout that the user set in the previous session. However not all geometry is saved, only the main window and not the children especially floating position of docked windows and table column widths within docked windows.
This thread seems to attempt to provide an answer (but in C++ and not python): http://www.qtforum.org/article/38362/save-geometry-of-all-widgets-recursively.html
I am coding in Python and my C++ is not very good so I can't fully follow the example at the above link.
Does anyone know if that approach works?
In general is it possible to use Qsettings to store floating position of docked windows and table column widths?
Could anyone suggest a python/pyside based solution?
Many thanks.
Ron
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')
This thread seems to attempt to provide an answer (but in C++ and not python): http://www.qtforum.org/article/38362/save-geometry-of-all-widgets-recursively.html
As three_pineapples pointed out, iterating over all widgets is not a good idea since things you wouldn't want to save, (buttons, label) are also derived from QWidget
.
I'd suggest to simply give meaningful name to each floating docked-window:
floatingWindow.setObjectName("floatingWindow")
Then you can iterate against object names instead seeking widgets from child object. To create a QSettings
group for that particular window:
QSettings settings
# Write geometry
settings.setValue("floatingWindow/size", floatingWindow.size())
settings.setValue("floatingWindow/pos", floatingWindow.pos())
# Read geometry
floatingWindow.setSize(settings.value("floatingWindow/size").toSize())
floatingWindow.setPos(settings.value("floatingWindow/pos").toPos())
You can make this easier by using common pattern for names, e.g. floatingMain
, floatingTools
, floatingMenu
.