Save Position and Size of the Application Window and load it for the next time
-
Hello i want to save the position and size of the application window to load it for the next time.
For Example:
I start my Application. I change the position of my window with mouse. I enlarge the window with my mouse. Now if i close my window and start my application next time again, i want to save the settings.Is there any possible to do that?
Best Regards
-
You should take a look at QWidget::saveGeometry() and QWidget::restoreGeometry().
-
@Christian-Ehrlicher i use the example in my constructor:
QSettings settings("MyCompany", "MyApp"); restoreGeometry(settings.value("myWidget/geometry").toByteArray()); restoreState(settings.value("myWidget/windowState").toByteArray());
and in my destructor:
QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState());
but it doesn't seem to work. I get always the same position and size.
Any suggestions ? -
Please read the links I gave you. In the dtor the widget is no longer visible and therefore there is no valid geometry anymore.
-
@developer_96 said in Save Position and Size of the Application Window and load it for the next time:
and in my destructor:
you should do it in the close event handler of your mainwindow for example
-
@raven-worx is it not possible with the destructor ? i mean its like a close event or not ?
@Christian-Ehrlicher yes i will thank you. -
@developer_96 said in Save Position and Size of the Application Window and load it for the next time:
i mean its like a close event or not ?
I wrote why it doesn't work...
-
@Christian-Ehrlicher right i undestand it know. And to solve the problem it would be ok to use my code in a close event ? and the part in the constructor is that ok ? I mean use it know in
void MainWindow::closeEvent(QCloseEvent* event) { QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); QMainWindow::closeEvent(event); }
and the reload in my constructor.
But it doesn't work -
@developer_96 Hi,
of course it doesn't work. You save values asgeometry
andwindowState
but - looking at your previous code - loadmyWidget/geometry
andmyWidget/windowState
.
In that notation loader expects a group, so sanitise your code please.Two remarks:
- if you use groups it might be more convenient to add
settings.beginGroup("myWidget")
before you start to read/write andsettings.endGroup()
once you done that part. - you init settings with
"MyCompany"
etc - it might be more convenient to put those in yourmain.cpp
file once before QApplication init and not bother afterwards, like this:
QCoreApplication::setApplicationVersion("Optional version string here"); QCoreApplication::setOrganizationName("MyCompany"); QCoreApplication::setOrganizationDomain("can be omitted on windows"); QCoreApplication::setApplicationName("MyApp"); QApplication a(argc, argv);
(editions: removed typos)
Cheers,
A. - if you use groups it might be more convenient to add
-
@artwaw hi,
thank you very much now its work. One problem is still remaining:
If i change the position its working.
If i do a fullscreen its still working.
But if I'm going to resize manually the application windows it does'nt work.
After close and reload i got the same size.
Maybe you can help me :) -
@developer_96 Odd. It should work, I use similar code in almost every project and it doesn't matter how you resize, it works.
From what you say though it looks like state is applied properly, the geometry is not (as maximised window is a state thing, not size).
If you're sure there is no typo between save and load of geometry I'd suggest clearing the settings (on Windows this involves registry, so please be careful) and trying again.
Also, you can try to put qDebug() on save and load to verify what is saved and loaded.
IncloseEvent()
:qDebug() << saveGeometry();
In ctor:qDebug() << settings.value("your geometry key").toByteArray();
-
@artwaw
so there is no typo. I'm sure.
Constructor:QSettings settings("MyCompany", "MyApp"); restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("windowState").toByteArray());
Close Event:
QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); QMainWindow::closeEvent(event);
What you mean with settings ? Would this solve my problem ?
-
@developer_96
qDebug() will output the data to the console so you can compare them - it might give you a hint what's wrong.I would modify
closeEvent()
though:QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); settings.sync(); // forces to write the settings to storage event->accept();
And try again.
-
@artwaw tried again. Not working. With debug() after close i got some hexa values. If im loading it, i got nothing. So "" is the only output.
-
@developer_96 said in Save Position and Size of the Application Window and load it for the next time:
So "" is the only output.
This means you load the data from the wrong location. Fix your settings as @artwaw already told you (and not only here in the forum)
-
@developer_96 like @Christian-Ehrlicher said, loader returns empty string which means data is being read from different location.
I'd strongly suggest editingmain.cpp
file like I hinted. Afterwards you just useQSettings settings;
. -
@artwaw @Christian-Ehrlicher i did it already. Modify the cpp like @artwaw said. It doesn't work. I mean if it would be a wrong location the fullscreen mode and the position mode wouldn't work too , or not ?
Regards
-
When the settings object returns an empty QByteArray but you saved a non-empty one you have a typo somewhere. Esp. when the same works for windowState. Check your code!
-
How can i check that it works for windowsState? I mean i commented out the windows state part and the fullscreen and positing changing is still working. If im going to commented out the geometry nothing is working.
@Christian-Ehrlicher Check more times there is no typo.
Hope you can help.
-
Provide a minimal, compilable example.