Qt - pyside - saveGeometry() saveState()

僤鯓⒐⒋嵵緔 提交于 2020-01-02 16:01:20

问题


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


回答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')



回答2:


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.



来源:https://stackoverflow.com/questions/34732872/qt-pyside-savegeometry-savestate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!