PDA

View Full Version : QMainWindow : restorestate problems



fmariusd
19th October 2009, 14:26
Hello,

i save the state of my main window and when i restore it's state it is not working well
my window contains some docked widgets
when i close a dockedWidget it creates a QPlaceHolderItem
and this QPlaceHolderItem is written in that byteArray that QmainWindow::savestate returns
and i think this is why if i have 2 docked widgets of the same type and one is closed
then i click save and then
when i restore the state none of them are showed , because QPlaceHolderItem messes things up .[i think]


Thanks.

Lykurg
19th October 2009, 14:32
Are the object names of your dock widgets unique?

fmariusd
19th October 2009, 14:38
yes they are

Lykurg
19th October 2009, 14:48
Ok, sorry for asking "stupid" questions, but normally it should work, so how do you save the state? Do you use QSettings for that or do you write the QByteArray yourself in a file? Maybe during saving and writing the bytes get corrupted. How your saving and lading looks like? And by the way which version of Qt do you use?

fmariusd
19th October 2009, 15:09
:) You are not asking stupid questions
I use QSettings and Qt 4.5.1

this is when i save the state



trdset.beginGroup("General");
trdset.setValue("STATE", saveState());
trdset.setValue("GEOM", saveGeometry());
trdset.endGroup();
trdset.sync();

loading the settings :


loadFunction (QSettings& trdset)
{

trdset.beginGroup("General");
QByteArray layout_data = trdset.value("STATE").toByteArray();
bool b= restoreState(layout_data);

}


I have put some breakpoints in qdockarealayout.cpp and i can see that when i save the state the code is saveing the qplaceholderitems too (qplaceholderitems for the closed docked widgets )

qdockarealayout.cpp : (it is entering this if when i save the state)



else if (item.placeHolderItem != 0) {
stream << (uchar) WidgetMarker;
stream << item.placeHolderItem->objectName;
uchar flags = 0;
....

fmariusd
19th October 2009, 15:55
i re -edited my first post :

.... and i think this is why if i have 2 docked widgets of the same type and one is closed
then i click save and then
when i restore the state none of them are showed , because QPlaceHolderItem messes things up .[i think]

Lykurg
19th October 2009, 16:21
Ok, I have tested it with various versions here and couldn't reproduce your problem. I have attached a project, please compile it and check if the error occurs even with that simple test application. Then we will see...

fmariusd
20th October 2009, 14:16
the difference between ur code mine is that i have this property set for the dockwidgets:


ui->d1->setAttribute(Qt::WA_DeleteOnClose, true);
ui->d2->setAttribute(Qt::WA_DeleteOnClose, true);

and i create them at runtime , and now it's almost working:

each time i close a dockwidget i must :


removeDockWidget(dk);

can i work with restore state like this?

fmariusd
20th October 2009, 15:17
i think i need a way to delete not used placeholderitems if someone knows ....

Lykurg
21st October 2009, 10:01
and i create them at runtime

From the docs:

bool QMainWindow::restoreDockWidget ( QDockWidget * dockwidget )
Restores the state of dockwidget if it is created after the call to restoreState(). Returns true if the state was restored; otherwise returns false.

Did this works?

fmariusd
30th October 2009, 08:05
that didn't work , when recreating the layout i had to recreate all the widgets even the deleted ones and than delete those once again and let just those which weren't closed when saving the state