PDA

View Full Version : Save and restore maximized state



Kwakkie
22nd June 2012, 09:26
Hello all

Currently using Qt 4.8.2 X64 on Windows, but I also tried it on 4.8.1 on a debian machine.

I'm wondering how you save the maximized state and restore it properly. Currently I save the position and size when closing and restore that when starting again, but this doesn't work all that well. The window looks maximized (the size is correct) but it's a little out of position and not really in maximized state (you can press the maximize button again).

Code isn't much, saving would be:


QSettings settings(COMPANY_NAME, PRODUCT_NAME);
settings.setValue("pos", pos());
settings.setValue("size", size());

Restoring would be:

QSettings settings(COMPANY_NAME, PRODUCT_NAME);
QPoint pos = settings.value("pos", QPoint(100, 100)).toPoint();
QSize size = settings.value("size", QSize(1024, 600)).toSize();
resize(size);
move(pos);


I also tried the saveGeometry() and restoreGeometry() functions. This works, except for a problem on a dual screen system where the window is maximized on the wrong screen (the default screen) at startup. So I thought I'd simply save the maximized state with isMaximized() as a bool and restore it with showMaximized(), but had similar results as well. Other 3rd party applications (e.g. visual studio) do this correct on my system.

So what is the correct way of doing this?

wysota
24th June 2012, 14:01
I would store isMaximized() and then call showMaximized() while restoring (instead of setting the size and position).

Kwakkie
24th June 2012, 19:04
Thanks for the reply. As mentioned above, that is one of the things I've tried (see the bottom of my first post). But it results in the program starting maximized on the wrong screen.

Anyone with other ideas on how to solve it?

wysota
24th June 2012, 19:22
So move it to the proper screen first.

Kwakkie
24th June 2012, 19:35
Yes I know that, but that is the question though. How do I detect the screen I'm on so I can save that information, and how do I move to the correct screen when restoring?

wysota
24th June 2012, 20:47
Use QDesktopWidget API.

Kwakkie
25th June 2012, 08:59
Thanks, that will solve it!

metalban
25th June 2012, 09:51
This bug appears with Qt 4.8.0 version.
You should vote in qt bug tracker, and hope will be solved a day.......
https://bugreports.qt-project.org/browse/QTBUG-21371
It' s not a severe bug, but it's annoying, for exampel Qt designer is never restored on 2nd screen if in maximized state...