restoreDockWidget reports true - but doesn't restore
-
Hi,
I have a QMainWindow and saving the state as per the docs (saveSateandrestoreState- and geometry). The docks all have unique objectName set. The are added after the constructor is called - but before the app gets going (i.e. before .exec())The
readSettingsis called in the c'tor of my main window (again copied from the docs and then when a dock is added restoreDockWidget is called and returnstruefor each dock. However it just seems to do nothing - and the state is as they were. i.e. if I move them round, exit and restart, then they are back where were originally.The ini file seems to be created (and updated) each time - state is obviously an opaque byte array (though it does have the dock names if one coverts to ascii!)
// aDock is created and object name set then passed to QMainWindow: aDock->setAllowedAreas(Qt::AllDockWidgetAreas); aDock->setAttribute(Qt::WA_DeleteOnClose, false); auto act = aDock->toggleViewAction(); act->setIcon(icon); displaysToolBar->addAction(act); addDockWidget(Qt::TopDockWidgetArea, aDock); restoreDockWidget(aDock)Is the above expected to work?
-
And it turns out I was calling windows Show before adding the docks :facepalm:
For those following at home - the above code minus the
readSettingsin the main window c'tor then addvoid showEvent(QShowEvent* event) override { readSettings(); QMainWindow::showEvent(event); } -
Hi,
I have a QMainWindow and saving the state as per the docs (saveSateandrestoreState- and geometry). The docks all have unique objectName set. The are added after the constructor is called - but before the app gets going (i.e. before .exec())The
readSettingsis called in the c'tor of my main window (again copied from the docs and then when a dock is added restoreDockWidget is called and returnstruefor each dock. However it just seems to do nothing - and the state is as they were. i.e. if I move them round, exit and restart, then they are back where were originally.The ini file seems to be created (and updated) each time - state is obviously an opaque byte array (though it does have the dock names if one coverts to ascii!)
// aDock is created and object name set then passed to QMainWindow: aDock->setAllowedAreas(Qt::AllDockWidgetAreas); aDock->setAttribute(Qt::WA_DeleteOnClose, false); auto act = aDock->toggleViewAction(); act->setIcon(icon); displaysToolBar->addAction(act); addDockWidget(Qt::TopDockWidgetArea, aDock); restoreDockWidget(aDock)Is the above expected to work?
-
Hi,
Can you provide a minimal example ?
The order of the operation you wrote are not clear.
To me it seems you are loading your settings too early with regard to how you construct your objects. -
Sure - this is the essence of it:
#include <QtWidgets> class MyDock: public QDockWidget { public: MyDock(const QString& title, const Qt::WindowFlags& flags = Qt::WindowFlags()) : QDockWidget(title, nullptr, flags) { setWidget(new QLabel("test")); } ~MyDock() override = default; }; class MyDockWindow : public QMainWindow { //Q_OBJECT public: MyDockWindow() { m_docksToolBar = addToolBar("docks"); addToolBar(Qt::LeftToolBarArea, m_docksToolBar); m_docksToolBar->setIconSize(QSize(40, 40)); m_docksToolBar->setMovable(false); m_docksToolBar->setFloatable(false); readSettings(); } ~MyDockWindow() = default; void addDock(const QString& id, MyDock* myDock, const QIcon& icon) { myDock->setAllowedAreas(Qt::AllDockWidgetAreas); myDock->setAttribute(Qt::WA_DeleteOnClose, false); myDock->setObjectName(id); auto act = myDock->toggleViewAction(); act->setIcon(icon); m_docksToolBar->addAction(act); addDockWidget(Qt::TopDockWidgetArea, myDock); //myDock->setVisible(false); qDebug("%s %d\n", id.toStdString().c_str(), restoreDockWidget(myDock)); } protected: void closeEvent(QCloseEvent* event) override { QSettings settings(QSettings::IniFormat, QSettings::UserScope, "dummy", "app"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); QMainWindow::closeEvent(event); } private: void readSettings() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, "dummy", "app"); restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("windowState").toByteArray()); } QToolBar* m_docksToolBar; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MyDockWindow w; auto aDock = new MyDock("hello 1"); w.addDock("hello1", aDock, QIcon::fromTheme(QIcon::ThemeIcon::Computer)); aDock = new MyDock("hello 2"); w.addDock("hello2", aDock, QIcon::fromTheme(QIcon::ThemeIcon::DriveOptical)); w.show(); return a.exec(); }The docks available will be configurable in the main app - hence adding this way. I did also try restoring state when the window is shown (i.e. after all docks have been added).
EDIT: AH! Hang on - if I try restoring state in the
showEventon the mainwindow in the above test app then it works. Will just try that in the real one. -
And it turns out I was calling windows Show before adding the docks :facepalm:
For those following at home - the above code minus the
readSettingsin the main window c'tor then addvoid showEvent(QShowEvent* event) override { readSettings(); QMainWindow::showEvent(event); } -
M Musmuris has marked this topic as solved on
-
It might not be the implementation you want,
showEventcan be called several times through the lifetime of your application so your GUI will give your user surprises when maximizing from a minimized state for example.I would recommend using a single shot QTimer with a 0 delay in your
main.cppto call readSettings.