Pieced together some quick POC code, but I believe it covers the major points. QDockWidgets do not appear to be saving state correctly with QMainWindow::saveState. Results vary with dock resizing, floating, and when not visible. I did not see this behavior in 4.1.4 open source, and tried to put together a quick poc rather then the large amount of code from my project for confirmation.
I am not using MiniGW and am unclear if this is a problem with patches to the open source version, 4.2 changes, my horrid programing, or some change I am missing. The test I ran with the code example was on a WinXP system.
main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include <QMainWindow>
#include <QDockWidget>
#include <QTextEdit>
Q_OBJECT
public:
MainWindow();
void readSettings();
void writeSettings();
protected:
};
#endif
#ifndef _MAIN_H_
#define _MAIN_H_
#include <QMainWindow>
#include <QDockWidget>
#include <QTextEdit>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
void readSettings();
void writeSettings();
QTextEdit *textEdit;
QDockWidget *d1;
QDockWidget *d2;
QDockWidget *d3;
protected:
void closeEvent(QCloseEvent *event);
};
#endif
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtGui>
#include <QApplication>
#include "main.h"
MainWindow::MainWindow() {
setCentralWidget(textEdit);
d1->setObjectName(tr("d1"));
d2->setObjectName(tr("d2"));
d3->setObjectName(tr("d3"));
addDockWidget(Qt::RightDockWidgetArea, d1);
addDockWidget(Qt::LeftDockWidgetArea, d2);
addDockWidget(Qt::TopDockWidgetArea, d3);
readSettings();
}
void MainWindow::readSettings() {
settings.beginGroup("session");
restoreState(settings.value("Layout").toByteArray());
QRect rect
= settings.
value("MainWindowGeometry",
QRect(0,
0,
780,
520)).
toRect();
move(rect.topLeft());
resize(rect.size());
settings.endGroup();
}
void MainWindow::writeSettings() {
settings.beginGroup("session");
settings.setValue("MainWindowGeometry", geometry());
settings.remove("Layout");
settings.setValue("Layout",saveState());
settings.endGroup();
}
{
writeSettings();
event->accept();
}
int main(int argc, char *argv[]) {
MainWindow mainwindow;
mainwindow.show();
return app.exec();
}
#include <QtGui>
#include <QApplication>
#include "main.h"
MainWindow::MainWindow() {
textEdit = new QTextEdit;
setCentralWidget(textEdit);
d1 = new QDockWidget(tr("Dock1"));
d1->setObjectName(tr("d1"));
d2 = new QDockWidget(tr("Dock2"));
d2->setObjectName(tr("d2"));
d3 = new QDockWidget(tr("Dock3"));
d3->setObjectName(tr("d3"));
addDockWidget(Qt::RightDockWidgetArea, d1);
addDockWidget(Qt::LeftDockWidgetArea, d2);
addDockWidget(Qt::TopDockWidgetArea, d3);
readSettings();
}
void MainWindow::readSettings() {
QSettings settings("poc.ini",QSettings::IniFormat);
settings.beginGroup("session");
restoreState(settings.value("Layout").toByteArray());
QRect rect = settings.value("MainWindowGeometry",
QRect(0, 0, 780, 520)).toRect();
move(rect.topLeft());
resize(rect.size());
settings.endGroup();
}
void MainWindow::writeSettings() {
QSettings settings("poc.ini",QSettings::IniFormat);
settings.beginGroup("session");
settings.setValue("MainWindowGeometry", geometry());
settings.remove("Layout");
settings.setValue("Layout",saveState());
settings.endGroup();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
writeSettings();
event->accept();
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow mainwindow;
mainwindow.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Some things you can try are:
1. If you close a dock, and reload it will not remember it is closed.
1a. Now if you had a dock nested in another and closed it, QMAinWindow::saveState will remember it was closed
2. Moving a dock to another position will be remember.
2a.Move a dock to another position and then close another, with 3 dockwidgets can cause all to be reset to default positioning.
3. Float one dock and close another, will cause all 3 to reset to default positioning.
Bookmarks