4
votes

I am writing Qt (4.8.1 on Ubuntu 12.04) application that stores it's main window geometry between sessions. I noticed that if widget is maximized, qt is not storing it's non-maximized geometry. Obviously I would like my application to return to it's non-maximized size just the same if it was closed/started since last maximization. In

  1. Main window is not maximized and has geometry X;
  2. maximize main window;
  3. save window geometry (using QWidget::saveGeometry) to config file;
  4. close my application;
  5. start it again;
  6. load geometry from config file
  7. Restore (un-maximize? ;)

After step 6 window gets maximized (as expected), but after step 7 it returns to some internal default size (i. e. one set while designing form in QtCreator), not to last non-maximized geometry X.

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

Is it because when maximized, the non maximized size is remembered by window manager and not qt (at least on linux)?

3

3 Answers

4
votes

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

To get your required functionality just modify your steps as follows:

  1. Main window is not maximized and has geometry X;
  2. Save Geometry X also left-top position of window as QPoint Y
  3. maximize main window;
  4. Do NOT save geometry (You can figure if window state is maximized using QWidget::isMaximized() before saving to config file). Save a new isMaximised state value to config file instead.
  5. close my application;
  6. start it again;
  7. Before you call window->show() apply a window->resize(lastQSizeSavedinSettingsofNonMaximisedState) and a window->move(lastQPointSavedinSettingsofNonMaximisedState)
  8. Now check the isMaximised state value from config and if true, just call QWidget::showMaximized() else just QWidget::show()
  9. Now when you restore window size, you should have your desired functionality :)

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

Always provide a fallback geometry and position in-case the last saved positions are out of bounds when the application is started and values you try to restore are not within the screen bounds anymore. (This helps catering for cases where someone changes resolution / monitor count / monitor position / virtual desktops)

1
votes
4. Do NOT save geometry (You can figure if window state is maximized using QWidget::isMaximized() before saving to config file). Save a new isMaximised state value to config file instead.

Another problem here is: A window will not just be maximized/minimized based on its position on the screen but based on where the greater part of the window is. If 80% of the window is on screen1 but the upper left corner is on screen 2, the maximized window will be on screen1.

Still, your idea is the best one. 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
votes

I think the issue you're having is coming from a number of geometries and sizes being readable and set-able for a QWidget. Specifically, you might want to look at the differences between normalGeometry, height, width, maximumHeight, maximumWidth, minimumHeight, minimumWidth etc.