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. Save Position and Size of the Application Window and load it for the next time

Save Position and Size of the Application Window and load it for the next time

Scheduled Pinned Locked Moved Solved General and Desktop
33 Posts 5 Posters 8.0k Views
  • 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 26 Feb 2021, 10:32 last edited by
    #4

    Please read the links I gave you. In the dtor the widget is no longer visible and therefore there is no valid geometry anymore.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    1
    • D developer_96
      26 Feb 2021, 10:29

      @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 ?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 26 Feb 2021, 10:34 last edited by
      #5

      @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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • D Offline
        D Offline
        developer_96
        wrote on 26 Feb 2021, 10:52 last edited by
        #6

        @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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 26 Feb 2021, 10:53 last edited by
          #7

          @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...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • D Offline
            D Offline
            developer_96
            wrote on 26 Feb 2021, 11:03 last edited by
            #8

            @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

            A 1 Reply Last reply 26 Feb 2021, 11:19
            0
            • D developer_96
              26 Feb 2021, 11:03

              @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

              A Offline
              A Offline
              artwaw
              wrote on 26 Feb 2021, 11:19 last edited by artwaw
              #9

              @developer_96 Hi,
              of course it doesn't work. You save values as geometry and windowState but - looking at your previous code - load myWidget/geometry and myWidget/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 and settings.endGroup() once you done that part.
              • you init settings with "MyCompany" etc - it might be more convenient to put those in your main.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.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              5
              • D Offline
                D Offline
                developer_96
                wrote on 26 Feb 2021, 11:53 last edited by
                #10

                @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 :)

                A 1 Reply Last reply 26 Feb 2021, 12:04
                0
                • D developer_96
                  26 Feb 2021, 11:53

                  @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 :)

                  A Offline
                  A Offline
                  artwaw
                  wrote on 26 Feb 2021, 12:04 last edited by
                  #11

                  @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.
                  In closeEvent(): qDebug() << saveGeometry();
                  In ctor: qDebug() << settings.value("your geometry key").toByteArray();

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    developer_96
                    wrote on 26 Feb 2021, 12:16 last edited by
                    #12

                    @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 ?

                    A 1 Reply Last reply 26 Feb 2021, 12:25
                    0
                    • D developer_96
                      26 Feb 2021, 12:16

                      @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 ?

                      A Offline
                      A Offline
                      artwaw
                      wrote on 26 Feb 2021, 12:25 last edited by
                      #13

                      @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.

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        developer_96
                        wrote on 26 Feb 2021, 12:29 last edited by
                        #14

                        @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.

                        C A 2 Replies Last reply 26 Feb 2021, 12:30
                        0
                        • D developer_96
                          26 Feb 2021, 12:29

                          @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.

                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 26 Feb 2021, 12:30 last edited by
                          #15

                          @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)

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          1
                          • D developer_96
                            26 Feb 2021, 12:29

                            @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.

                            A Offline
                            A Offline
                            artwaw
                            wrote on 26 Feb 2021, 12:33 last edited by
                            #16

                            @developer_96 like @Christian-Ehrlicher said, loader returns empty string which means data is being read from different location.
                            I'd strongly suggest editing main.cpp file like I hinted. Afterwards you just use QSettings settings;.

                            For more information please re-read.

                            Kind Regards,
                            Artur

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              developer_96
                              wrote on 26 Feb 2021, 12:44 last edited by
                              #17

                              @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

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 26 Feb 2021, 12:55 last edited by
                                #18

                                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!

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                1
                                • D Offline
                                  D Offline
                                  developer_96
                                  wrote on 26 Feb 2021, 12:59 last edited by
                                  #19

                                  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.

                                  A 1 Reply Last reply 26 Feb 2021, 13:01
                                  0
                                  • C Offline
                                    C Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 26 Feb 2021, 13:01 last edited by
                                    #20

                                    Provide a minimal, compilable example.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    1 Reply Last reply
                                    0
                                    • D developer_96
                                      26 Feb 2021, 12:59

                                      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.

                                      A Offline
                                      A Offline
                                      artwaw
                                      wrote on 26 Feb 2021, 13:01 last edited by
                                      #21

                                      @developer_96 you can use qDebug() to output saved and loaded values to the console, then compare them visually.

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      1 Reply Last reply
                                      1
                                      • D Offline
                                        D Offline
                                        developer_96
                                        wrote on 26 Feb 2021, 13:11 last edited by
                                        #22

                                        @Christian-Ehrlicher

                                        main.cpp:

                                          QCoreApplication::setOrganizationName("MyCompany");
                                          QCoreApplication::setApplicationName("MyApp");
                                          QApplication oApp(arc, arv);
                                        

                                        close event:

                                          QSettings settings("MyCompany", "MyApp");
                                          settings.setValue("geometry", saveGeometry());
                                          settings.setValue("windowState", saveState());
                                          settings.sync(); // forces to write the settings to storage
                                          event->accept();
                                        qDebug() << saveGeometry();
                                          qDebug() << saveState();
                                        

                                        constructor:

                                          QSettings settings("MyCompany", "MyApp");
                                          restoreGeometry(settings.value("geometry").toByteArray());
                                          restoreState(settings.value("windowState").toByteArray());
                                          qDebug() << settings.value("geometry").toByteArray();
                                          qDebug() << settings.value("windowState").toByteArray();
                                        

                                        @artwaw i tried again with debug.
                                        I got for geometry values, compared windows stats and geomtry. Same values in save and loaded.

                                        A 1 Reply Last reply 26 Feb 2021, 13:17
                                        0
                                        • D developer_96
                                          26 Feb 2021, 13:11

                                          @Christian-Ehrlicher

                                          main.cpp:

                                            QCoreApplication::setOrganizationName("MyCompany");
                                            QCoreApplication::setApplicationName("MyApp");
                                            QApplication oApp(arc, arv);
                                          

                                          close event:

                                            QSettings settings("MyCompany", "MyApp");
                                            settings.setValue("geometry", saveGeometry());
                                            settings.setValue("windowState", saveState());
                                            settings.sync(); // forces to write the settings to storage
                                            event->accept();
                                          qDebug() << saveGeometry();
                                            qDebug() << saveState();
                                          

                                          constructor:

                                            QSettings settings("MyCompany", "MyApp");
                                            restoreGeometry(settings.value("geometry").toByteArray());
                                            restoreState(settings.value("windowState").toByteArray());
                                            qDebug() << settings.value("geometry").toByteArray();
                                            qDebug() << settings.value("windowState").toByteArray();
                                          

                                          @artwaw i tried again with debug.
                                          I got for geometry values, compared windows stats and geomtry. Same values in save and loaded.

                                          A Offline
                                          A Offline
                                          artwaw
                                          wrote on 26 Feb 2021, 13:17 last edited by
                                          #23

                                          @developer_96 Please change QSettings invocation from QSettings settings("MyCompany", "MyApp"); to QSettings settings; in ctor and closeEvent()
                                          if you have same values on save and load then this should work. I am out of ideas for now.

                                          For more information please re-read.

                                          Kind Regards,
                                          Artur

                                          1 Reply Last reply
                                          0

                                          13/33

                                          26 Feb 2021, 12:25

                                          • Login

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