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.1k 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.
  • D Offline
    D Offline
    developer_96
    wrote on 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.

    Christian EhrlicherC artwawA 2 Replies Last reply
    0
    • D developer_96

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

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

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

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on 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 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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 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.

              artwawA 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                  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.

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on 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 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.

                    artwawA 1 Reply Last reply
                    0
                    • D developer_96

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

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on 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
                      • D Offline
                        D Offline
                        developer_96
                        wrote on last edited by developer_96
                        #24

                        oki i will try it and tell you.
                        Another question:
                        How it would work for a qwidget for example?

                          settings.setValue("qwidgetclassname/geometry", saveGeometry());
                          restoreGeometry(settings.value("qwidgetclassname/geometry").toByteArray());
                        ``
                        Maybe like that ??
                        artwawA 1 Reply Last reply
                        0
                        • D developer_96

                          oki i will try it and tell you.
                          Another question:
                          How it would work for a qwidget for example?

                            settings.setValue("qwidgetclassname/geometry", saveGeometry());
                            restoreGeometry(settings.value("qwidgetclassname/geometry").toByteArray());
                          ``
                          Maybe like that ??
                          artwawA Offline
                          artwawA Offline
                          artwaw
                          wrote on last edited by
                          #25

                          @developer_96 for particular widget it doesn't matter how you name it, string can be anything as long as it's unique. You can group it, you can save series as arrays, QSettings is really powerful.
                          But if you think of saving and restoring particular widgets let me tell you, there is really only a few cases when you need to do that. Qt way is to put your widgets into layouts on the window class and let Qt figure out sizes and positions once you restore global settings and state.
                          What's worth saving is for example when you have widgets placed on QSplitter - then it's worth saving splitter params.
                          Or sizes of sections of the header in views, should you want to.

                          Saving and restoring every widget's state etc is counter productive, using layouts is the right way.

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            developer_96
                            wrote on last edited by
                            #26

                            @artwaw right thank you for this info. I have a qtsplitter for two textboxes. So if im going to enlarge one box the other box i going to be smaller. What I want is: If im going to enlarge the one box and close the application, the same size should be there for both boxes.

                            artwawA 1 Reply Last reply
                            0
                            • D developer_96

                              @artwaw right thank you for this info. I have a qtsplitter for two textboxes. So if im going to enlarge one box the other box i going to be smaller. What I want is: If im going to enlarge the one box and close the application, the same size should be there for both boxes.

                              artwawA Offline
                              artwawA Offline
                              artwaw
                              wrote on last edited by
                              #27

                              @developer_96 Assuming you've done layouts right all you need to do is save/restore the splitter. I use that method in one of my projects, save/restore looks like this:

                              closeEvent(): settings.setValue("splitter",ui->splitter->saveState());

                              and i method loading the UI (can be ctor as well but this needs to be done once global state and geometry are restored):
                              ui->splitter->restoreState(settings.value("splitter").toByteArray());

                              Again - this works if you have your layouts done right. Otherwise it might work, it might not.

                              For more information please re-read.

                              Kind Regards,
                              Artur

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                developer_96
                                wrote on last edited by
                                #28

                                @artwaw nice thank you it is working. Really sad that the other problem is not solved :/

                                artwawA 1 Reply Last reply
                                0
                                • D developer_96

                                  @artwaw nice thank you it is working. Really sad that the other problem is not solved :/

                                  artwawA Offline
                                  artwawA Offline
                                  artwaw
                                  wrote on last edited by
                                  #29

                                  @developer_96 it must be something silly. From the code I work on currently:
                                  closeEvent():

                                      QSettings settings;
                                      settings.setValue("geo",saveGeometry());
                                      settings.setValue("state",saveState());
                                      settings.setValue("splitter",ui->splitter->saveState());
                                      settings.sync();
                                      event->accept();
                                  

                                  Ctor:

                                      QSettings settings;
                                      restoreGeometry(settings.value("geo").toByteArray());
                                      restoreState(settings.value("state").toByteArray());
                                      ui->splitter->restoreState(settings.value("splitter").toByteArray());
                                  

                                  For more information please re-read.

                                  Kind Regards,
                                  Artur

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    developer_96
                                    wrote on last edited by
                                    #30

                                    @artwaw i solved it ! it was only the problem that i use the methods befor the setupUi method.
                                    Thank you for your help !!

                                    artwawA Christian EhrlicherC 2 Replies Last reply
                                    1
                                    • D developer_96

                                      @artwaw i solved it ! it was only the problem that i use the methods befor the setupUi method.
                                      Thank you for your help !!

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on last edited by
                                      #31

                                      @developer_96
                                      Ah... happy you solved it.
                                      Please don't forget to mark topic as solved!

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      1 Reply Last reply
                                      0
                                      • D developer_96

                                        @artwaw i solved it ! it was only the problem that i use the methods befor the setupUi method.
                                        Thank you for your help !!

                                        Christian EhrlicherC Offline
                                        Christian EhrlicherC Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #32

                                        @developer_96 said in Save Position and Size of the Application Window and load it for the next time:

                                        it was only the problem that i use the methods befor the setupUi method.

                                        So when you would have posted a minimal, compilable example we would have seen this much earlier but you didn't want to...

                                        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
                                        • enjoysmathE Offline
                                          enjoysmathE Offline
                                          enjoysmath
                                          wrote on last edited by enjoysmath
                                          #33

                                          For those who still use PyQt5:

                                          ```
                                          
                                          _windowStatesEnum = {
                                              0x00000000 : Qt.WindowNoState,          #		The window has no state set (in normal state).
                                              0x00000001 : Qt.WindowMinimized,        #		The window is minimized (i.e. iconified).
                                              0x00000002 : Qt.WindowMaximized,	      #      The window is maximized with a frame around it.
                                              0x00000004 : Qt.WindowFullScreen,	      #      The window fills the entire screen without any frame around it.
                                              0x00000008 : Qt.WindowActive,		      # The window is the active window, i.e. it has keyboard focus.        
                                          }
                                              
                                          def __setstate__(self, data):
                                              self.__init__()
                                              self.setGeometry(data['geometry'])
                                              self.setWindowState(self._windowStatesEnum[data['window state']])
                                              
                                          def __getstate__(self):
                                              return {
                                                  'geometry' : self.geometry(),
                                                  'window state' : int(self.windowState()),
                                              }
                                          
                                          
                                          
                                          **```
                                          Some Qt5 types are pickleable, such as whatever is returned by .geometry().  For enum types this I find to be the simplest solution to over come that, except you might hide away the enum map in a tools file.  The above code goes inside a subclassed QMainWindow.  So you can pickle pretty much any class you want to except QApplication, and a few others.  So I just pickle the whole darn application.
                                          ```**

                                          https://github.com/enjoysmath
                                          https://math.stackexchange.com/users/26327/exercisingmathematician

                                          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