5

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.

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
user1899020
  • 13,167
  • 21
  • 79
  • 154

2 Answers2

21

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);
Nils
  • 2,041
  • 1
  • 15
  • 20
Michael
  • 320
  • 2
  • 5
  • 1
    resizing seems to work fine with that, but which dock is tabbed under which other tab etc. does not seem to be stored that way, unfortunately – IceFire Nov 27 '15 at 11:42
  • 2
    @IceFire: I had a similar issue but it turned out that I wasn't setting an object name for a dock widget and it was causing the layout to do unexpected things. There was actually an error on settings save I didn't notice at first (using PySide 1.2.4 with Qt 4.8.6) – Rafe Sep 05 '17 at 00:50
-3

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.

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
  • 2
    This answer's provably wrong. KDE programs even save window state by default, through their KXMLGUI framework. On the Qt level, Michael's answer's the correct one. – Stefan Majewsky Apr 25 '13 at 12:39
  • @StefanMajewsky - Good catch. I'd based this on a Qt Project thread where one of the developers was saying it couldn't be done... Old info, apparently. I'll go upvote Michael. – Xavier Holt Apr 29 '13 at 22:18