Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] save and restore window / dialog positions
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] save and restore window / dialog positions

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 18.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #3

    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.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWidget>
    #include <QSettings>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QSettings settings;

    restoreGeometry(settings.value("mainWindowGeometry").toByteArray());
    
    // create docks, toolbars, etc...
    
    restoreState(settings.value("mainWindowState").toByteArray());
    

    }

    void closeEvent(QCloseEvent *event) {
    QSettings settings;
    settings.setValue("mainWindowGeometry", saveGeometry());
    settings.setValue("mainWindowState", saveState());
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #4

      Line 22 should be:

      @
      void MainWindow::closeEvent(QCloseEvent *event) {
      @

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #5

        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?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalster
          wrote on last edited by
          #6

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

          @ QCoreApplication::setOrganizationDomain("OrgDomain");
          QCoreApplication::setOrganizationName("OrgName");
          QCoreApplication::setApplicationName("AppName");
          QCoreApplication::setApplicationVersion("1.0.0");@

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #7

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

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #8

              ok, I will. thank you

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on last edited by
                #9

                "here":http://developer.qt.nokia.com/doc/qt-4.7/qapplication.html#id-7292984e-d337-4916-bbdf-6b5499d193c1 is the link from the save state quote below
                [quote]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.[/quote]

                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?

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #10

                  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.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on last edited by
                    #11

                    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?

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      redkite
                      wrote on last edited by
                      #12

                      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.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kalster
                        wrote on last edited by
                        #13

                        thank you redkite. i marked this topic as solved.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved