Preserving/Restoring QDockWidget size
Unsolved
General and Desktop
-
@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!