tags:

views:

437

answers:

3

Hello,

I want to save my custom DockWidgets (inherited from QDockWidget) with the saveState() / restoreState() function my MainWindow provides.

I have two questions: 1. How can I save and restore my Dockwidgets?
- I already tried registering my custom DockWidgets as a QMetaType and implementing the default Constructor, copy Constructor, Destructor and Streaming operators.
2. How can I identify the loaded DockWidgets?
- For example: If 2 DockWidgets where saved and I load them with restoreState(), is there a way to get pointers to these loaded Widgets?

Thanks, Tobias

+1  A: 

I already solved question 2:
very easy .. QList<QDockWidget*> dockWidgetList = findChildren<QDockWidget*>();
But the list is currently empty after loading, because there is no widget being loaded ;)

Tobias
+1  A: 

Did you read the QMainWindow::saveState documentation?

Are your objects uniquely named? (QObject::setObjectName)

As a side note, QObjects should NOT have a copy constructor, see Q_DISABLE_COPY

Adam W
yes, I read the documentation but I think it is very short and does not tell me very much about.. saveState seems to work I think, the state variable is save to the INI-File. But the DockWidgets seem not to be saved there, too...yes, I just added a static counter which is appended to all object names, just for testing purposes.I added the copy constructor because I thaught, it may work like registering custom meta types for serialization.Thank you for this information about the copy constructor!
Tobias
Did you call setObjectName on the instances of your dock widgets? That is very important as Qt uses those names for many things. Does restoring from the ini file works for everything else? If not, I would check the code you use for saving and restoring since saveState returns a QByteArray that could get clobbered by file encoding.
Adam W
yes, I create my DockWidgets and after that call setObjectName(name) on each of them with a unique name.Saving is currently done with settings->setValue("mainwindow/state", this->saveState());Do I have to encode/decode the QByteArray before writing it to the ini? I used the same code as in the documentation..The size and position of my Toolbar is saved and loaded every time so I think that it is working but my custom DockWidgets won't be restored (I don't know how to check if they are saved).
Tobias
As long as some things are being restored correctly, I wouldn't worry about encoding. At this point it seems like you might have to write your own save and restore state functions if you really want to derive from `QDockWidget`.
Adam W
I think I got something going .. I will save my unique opject names together with all other necessary parameters for this DockWidget and the child Widget in my settings.While looping through them I create the DockWidgets on my own but do not use the addDockWidget() function, but the restoreDockWidget() function. If I set the same unique object names, the size and position is restored corretly.. Maybe restoreState() cannot create classes other than QDockWidget .. maybe its a bug .. I don't really know ;)
Tobias
+1  A: 

It seems that you misunderstood something about restoreState - it will not recreate your dock widgets (how could it do anyway?) but will just restore their position and status (hidden, free or docked). You still have to create your dock widgets manually before invoking restoreState.

Gnurou
Okay, thank you! I thought it can serialize these Widgets in some way or save the classes they were made of to recreate them automatically. So I have to save a list of all open DockWidgets for myself on close and create the corresponding DockWidgets again on Startup before calling the restoreState() I think..
Tobias