Qt Forum

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

    [SOLVED]About restoring the Mainwindow not for the first time

    General and Desktop
    3
    4
    1859
    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.
    • Z
      zhanguoxiaoyan last edited by

      The problem is that how to set a defult size and state only for the first time? After the first time, the state and size are used the settings for the last time.

      I have implemented the function to store the current size and splitter state. And in the constructor, call the RestoreState function instead of setting as default state and size.

      However, at the first time when running the program, the size is too small, just because of without stored size and state at the first time. How to solve this problem?

      The following is the code I have implemented,

      @// store state when close
      void MainWindow::closeEvent(QCloseEvent *event)
      {
      QSettings settings("xxx", "xxxxx");
      settings.beginGroup("MainWindow");
      settings.setValue("size", size());
      settings.setValue("pos", pos());
      settings.setValue("splitterState", ui->splitter->saveState());
      settings.endGroup();
      }@

      @// restore the state and size in the constructor
      void MainWindow::readSettings()
      {
      QSettings settings("xxx", "xxxxx");
      settings.beginGroup("MainWindow");
      resize(settings.value("size").toSize());
      move(settings.value("pos").toPoint());
      ui->splitter->restoreState(settings.value("splitterState").toByteArray());
      settings.endGroup();
      }@

      I think, the key point is that how to let the program know whether it is the first running or not.

      Thanks!

      1 Reply Last reply Reply Quote 0
      • M
        mlong last edited by

        See if the size returned is a valid QSize. If not, assume it's the first time.

        @
        void MainWindow::readSettings()
        {
        QSettings settings("xxx", "xxxxx");
        settings.beginGroup("MainWindow");

        QSize size = settings.value("size").toSize();
        
        if (size.isValid())  
        {  
           resize(size);
           move(settings.value("pos").toPoint());
           ui->splitter->restoreState(settings.value("splitterState").toByteArray());
        }
        else
        {
            // handle first time usage.
        }
        settings.endGroup();
        

        }
        @

        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 Reply Quote 0
        • M
          MuldeR last edited by

          Well, either store some "FirstRun" flag in your QSettings object, just like you store the other options.

          ...or set a reasonable default value for the "size" option.

          @QSize defaultSize(800, 600); //Adjust default as needed!
          resize(settings.value("size", defaultSize).toSize()); //Pass a default to QSettings::value()@

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            [quote author="MuldeR" date="1335282358"]Well, either store some "FirstRun" flag in your QSettings object, just like you store the other options.

            ...or set a reasonable default value for the "size" option.

            @QSize defaultSize(800, 600); //Adjust default as needed!
            resize(settings.value("size", defaultSize).toSize()); //Pass a default to QSettings::value()@[/quote]

            That's a fine approach too. For the record, I had used the valid test in order to be able to create a block of code that he could use to do any other "first-time" initialization. They're all valid solutions, though.

            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 Reply Quote 0
            • First post
              Last post