Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    5
    33
    1622
    Loading More Posts
    • 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.
    • artwaw
      artwaw @developer_96 last edited by artwaw

      @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 Reply Quote 5
      • D
        developer_96 last edited by

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

        artwaw 1 Reply Last reply Reply Quote 0
        • artwaw
          artwaw @developer_96 last edited by

          @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 Reply Quote 0
          • D
            developer_96 last edited by

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

            artwaw 1 Reply Last reply Reply Quote 0
            • artwaw
              artwaw @developer_96 last edited by

              @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 Reply Quote 0
              • D
                developer_96 last edited by

                @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 Ehrlicher artwaw 2 Replies Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion @developer_96 last edited by

                  @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 has to stay free or it will die.

                  1 Reply Last reply Reply Quote 1
                  • artwaw
                    artwaw @developer_96 last edited by

                    @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 Reply Quote 0
                    • D
                      developer_96 last edited by

                      @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 Reply Quote 0
                      • Christian Ehrlicher
                        Christian Ehrlicher Lifetime Qt Champion last edited by

                        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 has to stay free or it will die.

                        1 Reply Last reply Reply Quote 1
                        • D
                          developer_96 last edited by

                          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.

                          artwaw 1 Reply Last reply Reply Quote 0
                          • Christian Ehrlicher
                            Christian Ehrlicher Lifetime Qt Champion last edited by

                            Provide a minimal, compilable example.

                            Qt has to stay free or it will die.

                            1 Reply Last reply Reply Quote 0
                            • artwaw
                              artwaw @developer_96 last edited by

                              @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 Reply Quote 1
                              • D
                                developer_96 last edited by

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

                                artwaw 1 Reply Last reply Reply Quote 0
                                • artwaw
                                  artwaw @developer_96 last edited by

                                  @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 Reply Quote 0
                                  • D
                                    developer_96 last edited by 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 ??
                                    artwaw 1 Reply Last reply Reply Quote 0
                                    • artwaw
                                      artwaw @developer_96 last edited by

                                      @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 Reply Quote 0
                                      • D
                                        developer_96 last edited by

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

                                        artwaw 1 Reply Last reply Reply Quote 0
                                        • artwaw
                                          artwaw @developer_96 last edited by

                                          @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 Reply Quote 0
                                          • D
                                            developer_96 last edited by

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

                                            artwaw 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post