1

I'm struggling with QMainWindow and QDockWidgets, it seems a messy area.

I need the very simple thing: when application is closed, the arrangement of dockwidgets should be saved, and when application is opened, the arrangement should be restored.

The problem is that saveState()/restoreState() methods deal very strangely with central widget. There are several variants, and each has its own problems:

1. Central widget is set to, say, QPlainTextEdit or QLabel

When state is restored, dockwidgets occupy as much horizontal space as possible, with thin central widget in the middle of left and right dockwidgets. Size and position of central widget is kept only vertically, but horizontal boundaries between dockwidgets and central widget is not saved.

2. Central widget is set to NULL

If I explicitly call:

this->setCentralWidget(NULL);

Then stateSave()/stateRestore() do work: state is saved and restored correctly, but for some strange reason user is unable to dock widgets at the top or bottom. Widgets can be docked only in one horizontal row, no matter how hard I try to dock it at the top or bottom.

3. No central widget

By "no central widget" I mean not touching central widget at all. For some reason, the behavior is different comparing to NULL central widget:

State is saved and restored correctly, but if I make my window small, and then expand it again, then dockwidgets will remain small with huge unused area in the middle. So, user will have to resize them manually.


All of this looks very strange. Probably I'll have to go with "no central widget" variant, but I'm confused because I don't understand the behavior, maybe it's a bug, and then, behavior may change in future versions.

So, each of these ways has its own problems. If anyone can give me some clue on solving any of them, I'd be very glad.

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • If your main window is constructed using Qt Designer, it always adds a central widget even if there is no other content. That may explain difference between cases 2 and 3. As opposite, if you just create a `QMainWindow`, its central widget will be NULL by default. What is your Qt version and OS? – Pavel Strakhov Jul 05 '15 at 23:06
  • Hmm, `QMainWindow` is created by default by Qt Creator (default form). I tried Qt 5.4.1 and 5.5.0, on both Linux Mint and Windows 7. Behavior is the same. (but at 5.5.0 window resize is happening significantly slower than 5.4.1, by the way, but this is unrelevant here) – Dmitry Frank Jul 06 '15 at 02:04

0 Answers0