Preserving/Restoring QDockWidget size
-
@Perdrix Maybe I am trying to restore the geometry and window state at the wrong time? I am doing it in code invoked from showEvent()
void DeepSkyStacker::showEvent(QShowEvent* event) { if (!event->spontaneous()) { if (!initialised) { initialised = true; onInitialise(); } } // Invoke base class showEvent() return Inherited::showEvent(event); }
-
@Perdrix
Hi David,
If I tweak the reproducer and overrideQMainWindow::showEvent(QShowEvent *event)
, it works as well.I don't know about the
DeepSkyStacker
class, where it inherits from and when its first non-spontaneuous show event is consumed. My gut feeling tells me to put some pocket money on the show event. It's an unusual place to callQMainWindow::restoreState()
: Are the dock widgets are already constructed and added to the main window, when the show event is consumed? The dock widget part of the call will be a no-op, if the dock widgets aren't in place or don't have an object name.I'd happily look into the code of DSS, because it's a great piece of software (have I ever mentioned that? Apologies if I haven't). It's a VS project, however, and I only use Qt Creator.
I'd try the following:
- play with the reproducer in my earlier post, to see if the issue can be isolated
- play with the dock widgets: Tab them, undock them, close and restart the app => see if anything is restored at all.
- move the the code calling
QMainWindow::restoreState()
right after the construction of the dock widgets.
Cheers
Axel -
@Axel-Spoerl The widgets are also constructed in onInitialise().
You suggest that showEvent() is an unusual place from which to call restoreState(). Where would you normally put it? At the end of the ctor as you did in you small example?
-
@Perdrix said in Preserving/Restoring QDockWidget size:
At the end of the ctor as you did in you small example?
The c'tor is definitely a good spot, but it has to be called after the dock widgets are fully created.
-
@Axel-Spoerl Moved the code to the ctor and all now works!
The final code now reads:
ZTRACE_RUNTIME("Restoring Window State and Position"); QSettings settings{}; settings.beginGroup("MainWindow"); if (settings.contains("geometry") && settings.contains("maximised")) { const QByteArray geometry{ settings.value("geometry").toByteArray() }; const bool maximised{ settings.value("maximised").toBool() }; if (maximised) { showMaximized(); setGeometry(screen()->availableGeometry()); } else { restoreGeometry(geometry); } } if (settings.contains("windowState")) { auto windowState{ settings.value("windowState").toByteArray() }; restoreState(windowState); } settings.endGroup();
Thanks
David -
@Perdrix
Hey David! Great! Glad that it works!
Just two minor nitpicks:- no need to instantiate settings with curly braces.
- if the settings object is meant to be only for reading settings, I would constify it. I've had strange bugs where settings became automagically written. E.g. in your case, an empty "MainWindow" group would be created. Such things are hard to debug, hence my constification paranoia.
-
@Axel-Spoerl Thanks for all the help.
If I made the settings object const, I suspect I couldn't issue settings.beginGroup()? Or does that change a mutable member of the class?
-
@Perdrix
Good point, you are right.beginGroup()
isn't const. So my advice is bad. Apologies, I'll take it back. -
@Axel-Spoerl
There is something wrong (lacking) inQSettings
if the only reason a non-const
can't be used to read is that you are forced to usebeginGroup()
.Can one make a key to pass to
value()
to bypass the need tobeginGroup()
? -
@JonB
Fully agree. QSettings deserves some love for sure. -
-
This post is deleted!