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]About restoring the Mainwindow not for the first time
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.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.
  • Z Offline
    Z Offline
    zhanguoxiaoyan
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          [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
          0

          • Login

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