QWidget :: save / restoreGeometry loses window size, if maximized - error or function?

I am writing Qt (4.8.1 on Ubuntu 12.04) that stores the main window geometry between sessions. I noticed that if the widget is maximized, qt does not preserve its non-maximized geometry. Obviously, I would like my application to return to it a non-maximized size as well if it was closed / started since the last maximization. IN

  • The main window is not maximized and has X geometry;
  • enlarge the main window;
  • save window geometry (using QWidget :: saveGeometry) in the configuration file;
  • close my application;
  • run it again;
  • geometry of loading from a configuration file
  • Recover (un-maximize ?;)

After window 6 is maximized (as expected), but after step 7, it will return to some internal default size (i.e., one set when designing the form in QtCreator), and not to the length of the non-maximized X geometry.

Is this the desired behavior? Or is it impossible / hard to implement inside qt?

Is this because when maximizing the size of the non-maximized value, it is remembered by the window manager rather than qt (at least on linux)?

+4
source share
3 answers

You do not need to save geometry when the window is maximized to begin with.

To get the required functions, just change your actions as follows:

  • The main window is not maximized and has X geometry;
  • Save X geometry also in the upper left position of the window as QPoint Y
  • enlarge the main window;
  • Do NOT save the geometry (you can see if the window state is maximized with QWidget::isMaximized() before saving to the configuration file). Save the new isMaximised status isMaximised for the configuration file.
  • close my application;
  • run it again;
  • Before calling window->show() apply window->resize(lastQSizeSavedinSettingsofNonMaximisedState) and window->move(lastQPointSavedinSettingsofNonMaximisedState)
  • Now check the isMaximised status isMaximised from config and if true, just call QWidget :: showMaximized () else just QWidget::show()
  • Now, when you restore the window size, you should have the desired functionality :)

Something to keep in mind when dealing with window size / states.

Always provide backup geometry and position in case the last saved positions are outside the time limits when the application is running and the values ​​you are trying to restore are no longer on the screen. (This helps to service cases when someone changes the resolution / monitor counter / monitor position / virtual desktops)

+4
source

I think the problem you are facing comes from the many geometries and sizes that are read and configured for QWidget. In particular, you can see the differences between normalGeometry, height, width, maximumHeight, maximumWidth, minimumHeight, minimumWidth, etc.

0
source
4. DO NOT save the geometry (you can see if the window state is maximized using QWidget :: isMaximized () before saving to the configuration file). Save the new isMaximised state value in the configuration file.

Another problem is this: the window will not only be maximized / minimized depending on its position on the screen, but depending on where most of the window is located. If 80% of the window is on screen1, but the upper left corner is on screen 2, the maximum window will be on screen1.

However, your idea is the best. After over an hour of Google (using QT5), I now use:

writeSettings:

 settings.setValue("pos", pos()); if(!isMaximized()) settings.setValue("size", size()); settings.setValue("maximized", isMaximized()); 

readSettings:

 if(settings.contains("pos")) move(settings.value("pos").toPoint()); if(settings.contains("size")) resize(settings.value("size").toSize()); if(settings.value("maximized").toBool()) setWindowState(windowState() | Qt::WindowMaximized); 
0
source

All Articles