Any easy way to store dock widows layout and sizes in settings with Qt?

早过忘川 提交于 2019-12-23 12:46:51

问题


I have a main window with some internal dockable windows. I can move, resize and redock those dockable windows. After close and reopen the program, I want the moves, sizes and redocking are kept. Any easy way to implement it? I think it will use settings. But which info should be saved in settings. And how to set a default layout of all these dock windows? When click an action button, it can be restored. Thanks.


回答1:


Unfortunately, no, there is no built-in way to do this.

You'll need to loop through all your toolbars and dockable widgets and write their positions (and possibly their visibility) to a file. To restore, you can read that file and set your positions based on what you read.

The good news is that once you have such a system set up, making a default layout is easy - move all your widgets where you want them, then save your layout file, just like you do the user layouts above. You can even have multiple layouts, as long as they all get separate files.




回答2:


Check out QMainWindow::saveState/restoreState. It does exactly this.

To Save:

QSettings settings;
settings.setValue("DOCK_LOCATIONS",this->saveState(SOME_VERSION_DEFINE));

To Restore:

QSettings settings;
this->restoreState(settings.value("DOCK_LOCATIONS").toByteArray(),SOME_VERSION_DEFINE);


来源:https://stackoverflow.com/questions/14288635/any-easy-way-to-store-dock-widows-layout-and-sizes-in-settings-with-qt

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