PDA

View Full Version : How to save/restore DockWidgets created in the course of an application?



cgahete
18th September 2012, 16:50
Hello all,

Qt allows recovering the position and state of toolbars and QDockWidget using Qsettings. However, if you create in the course of an application a QDockWidget clicking a button and write the settings then, you can't restore the state of this QDockWidget. Can anyone give me a solution?

Qt code:

#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow mainWin;
mainWin.resize(800,600);
mainWin.show();

return app.exec();
}

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QListWidget;
class QMenu;
class QTextEdit;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

protected:
void closeEvent(QCloseEvent *event);

private slots:
void createDelayDockWindow();


private:
void createActions();
void createMenus();
void createToolBars();
void createStatusBar();
void createDockWindows();

void readSettings();
void writeSettings();


QListWidget *dockList;
QListWidget *delaydockList;


QMenu *fileMenu;
QMenu *editMenu;
QMenu *viewMenu;
QMenu *helpMenu;
QToolBar *fileToolBar;
QToolBar *editToolBar;

QAction *quitAct;

};


#endif

#include <QtGui>

#include "mainwindow.h"

MainWindow::MainWindow()
{
QWidget *window=new QWidget();
QVBoxLayout *vlayout= new QVBoxLayout;
QTextEdit *text1=new QTextEdit("Text1");
QPushButton *button1 = new QPushButton("New Dock Window");
connect(button1, SIGNAL(clicked()),this, SLOT(createDelayDockWindow()));

vlayout->addWidget(text1);
vlayout->addWidget(button1);
window->setLayout(vlayout);
setCentralWidget(window);

createActions();
createMenus();
createToolBars();
createStatusBar();
createDockWindows();

readSettings();

setWindowTitle(tr("Dock Widgets"));

}

void MainWindow::closeEvent(QCloseEvent *event)
{
writeSettings();

}

void MainWindow::createActions()
{
quitAct = new QAction(tr("&Quit"), this);
quitAct->setShortcuts(QKeySequence::Quit);
quitAct->setStatusTip(tr("Quit the application"));
connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));

}

void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));

fileMenu->addAction(quitAct);

editMenu = menuBar()->addMenu(tr("&Edit"));


viewMenu = menuBar()->addMenu(tr("&View"));

menuBar()->addSeparator();

helpMenu = menuBar()->addMenu(tr("&Help"));

}

void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(quitAct);

editToolBar = addToolBar(tr("Edit"));

}


void MainWindow::createStatusBar()
{
statusBar()->showMessage(tr("Ready"));
}



void MainWindow::createDockWindows()
{
// DOCK WIDGET
QDockWidget *dock = new QDockWidget(tr("Dock1"), this);
dock->setObjectName(tr("Dock1"));
dockList = new QListWidget(dock);

dock->setWidget(dockList);
addDockWidget(Qt::RightDockWidgetArea, dock);
viewMenu->addAction(dock->toggleViewAction());

}

void MainWindow::createDelayDockWindow(){
// DELAYED DOCK WIDGET
QDockWidget *newdock = new QDockWidget(tr("Dock Delayed"), this);
newdock->setObjectName(tr("Dock Delayed"));
delaydockList = new QListWidget(newdock);
newdock->setWidget(delaydockList);

addDockWidget(Qt::TopDockWidgetArea, newdock);
newdock->setFloating(true);

viewMenu->addAction(newdock->toggleViewAction());

}

void MainWindow::writeSettings(){

QSettings settings("Test", "Test Dock Problem");
settings.setValue("MainWindow/State", saveState());

settings.setValue("MainWindow/Geometry", saveGeometry());
}


void MainWindow::readSettings(){
QSettings settings("Test", "Test Dock Problem");
restoreGeometry(settings.value("MainWindow/Geometry").toByteArray());
restoreState(settings.value("MainWindow/State").toByteArray());
}