August 5, 2011

kalster kalster
Lab Rat
233 posts

[SOLVED] save and restore window / dialog positions

 

I would like to save my mainwindow, dialogs and toolbar positions. I am having a difficult time implementing the save and restore geometry code found here. [developer.qt.nokia.com] the problem is that i don’t know how to create the savegeometry and savestate functions.

can I have a working example that works with x11 please.

12 replies

August 5, 2011

mlong mlong
Robot Herder
1537 posts

You don’t have to create them. They’re already provided by QWidget [doc.qt.nokia.com] (unless you’re using a version of Qt older than 4.2). If your class inherits QWidget, then you can just call the methods.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 5, 2011

kalster kalster
Lab Rat
233 posts

I am have a problem with line 24 / 25 of the code. I am getting the error mainwindow.cpp:24: error: ‘saveGeometry’ was not declared in this scope and mainwindow.cpp:25: error: ‘saveState’ was not declared in this scope.

do i have to create the saveGeometry and saveState functions? I need examples for this code please.

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QWidget>
  4. #include <QSettings>
  5.  
  6.  
  7. MainWindow::MainWindow(QWidget *parent) :
  8.     QMainWindow(parent),
  9.     ui(new Ui::MainWindow)
  10. {
  11.     ui->setupUi(this);
  12.     QSettings settings;
  13.  
  14.     restoreGeometry(settings.value("mainWindowGeometry").toByteArray());
  15.  
  16.     // create docks, toolbars, etc...
  17.  
  18.     restoreState(settings.value("mainWindowState").toByteArray());
  19. }
  20.  
  21. void closeEvent(QCloseEvent *event) {
  22.     QSettings settings;
  23.     settings.setValue("mainWindowGeometry", saveGeometry());
  24.     settings.setValue("mainWindowState", saveState());
  25. }
  26.  
  27. MainWindow::~MainWindow()
  28. {
  29.     delete ui;
  30. }

August 5, 2011

mlong mlong
Robot Herder
1537 posts

Line 22 should be:

  1. void MainWindow::closeEvent(QCloseEvent *event) {

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 5, 2011

kalster kalster
Lab Rat
233 posts

thank you Mlong.

I changed line 22. run the program and i do not get any errors. Yet the code is not saving the main window position. what am i doing wrong?

August 5, 2011

kalster kalster
Lab Rat
233 posts

i solved it. i needed the following code in the main.cpp file

  1.  QCoreApplication::setOrganizationDomain("OrgDomain");
  2.     QCoreApplication::setOrganizationName("OrgName");
  3.     QCoreApplication::setApplicationName("AppName");
  4.     QCoreApplication::setApplicationVersion("1.0.0");

August 5, 2011

mlong mlong
Robot Herder
1537 posts

Glad it’s working! (Be sure and use meaningful values in your Organization & App domains and names.)

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 5, 2011

kalster kalster
Lab Rat
233 posts

ok, I will. thank you

August 5, 2011

kalster kalster
Lab Rat
233 posts

here [developer.qt.nokia.com] is the link from the save state quote below

This function deals with session management. It is invoked when the session manager wants the application to preserve its state for a future session.

For example, a text editor would create a temporary file that includes the current contents of its edit buffers, the location of the cursor and other aspects of the current editing session.

You should never exit the application within this function. Instead, the session manager may or may not do this afterwards, depending on the context. Futhermore, most session managers will very likely request a saved state immediately after the application has been started. This permits the session manager to learn about the application’s restart policy.

one last question. above is a quote about saveStates. the savestate code that I have in my program (see below code), will my program exit within this saveState function?

  1. settings.setValue("mainWindowState", saveState());

August 5, 2011

mlong mlong
Robot Herder
1537 posts

I’m not sure exactly what you’re asking.

The saveState() won’t ever cause your program to exit.

However, when the above program exits, the closeEvent() will be called as the window is closed and, thus, saveState() will be called.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 5, 2011

kalster kalster
Lab Rat
233 posts

the save state quote above is saying that You should never exit the application within this save state function. is my code exiting within the save state function?

August 5, 2011

redkite redkite
Lab Rat
23 posts

no, your code is exiting after saveState(), when closeEvent() is ended.

This warning is only for people who re-implement the saveState() fucntion, it’s not your case, you are using the original one.

August 6, 2011

kalster kalster
Lab Rat
233 posts

thank you redkite. i marked this topic as solved.

 
  ‹‹ Segmentation fault, SIGSEGV      [SOLVED] saving the result of a check box ››

You must log in to post a reply. Not a member yet? Register here!