KConfig 5.109.0
Public Member Functions | List of all members
KWindowStateSaver

Saves and restores a window size and (when possible) position. More...

#include <kwindowstatesaver.h>

Public Member Functions

 KWindowStateSaver (QWindow *window, const char *configGroupName)
 Create a new window state saver for window.
 
 KWindowStateSaver (QWindow *window, const KConfigGroup &configGroup)
 Create a new window state saver for window.
 
 KWindowStateSaver (QWindow *window, const QString &configGroupName)
 Create a new window state saver for window.
 
template<typename Widget >
 KWindowStateSaver (Widget *widget, const char *configGroupName)
 Create a new window state saver for widget.
 
template<typename Widget >
 KWindowStateSaver (Widget *widget, const KConfigGroup &configGroup)
 Create a new window state saver for widget.
 
template<typename Widget >
 KWindowStateSaver (Widget *widget, const QString &configGroupName)
 Create a new window state saver for widget.
 

Detailed Description

Saves and restores a window size and (when possible) position.

This is useful for retrofitting persisting window geometry on existing windows or dialogs, without having to modify those classes themselves, or having to inherit from them. For this, create a new instance of KWindowStateSaver for every window that should have it's state persisted, and pass it the window or widget as well as the config group the state should be stored in. The KWindowStateSaver will restore an existing state and then monitor the window for subsequent changes to persist. It will delete itself once the window is deleted.

QPrintPreviewDialog dlg = ...
new KWindowStateSaver(&dlg, "printPreviewDialogState");
...
dlg.exec();
Saves and restores a window size and (when possible) position.
Definition kwindowstatesaver.h:47

Note that freshly created top-level QWidgets (such as the dialog in the above example) do not have an associated QWindow yet (ie. windowHandle() return nullptr). KWindowStateSaver supports this with its QWidget constructors which will monitor the widget for having its associated QWindow created before continuing with that.

When implementing your own windows/dialogs, using KWindowConfig directly can be an alternative.

See also
KWindowConfig
Since
5.92

Constructor & Destructor Documentation

◆ KWindowStateSaver() [1/6]

KWindowStateSaver::KWindowStateSaver ( QWindow *  window,
const KConfigGroup configGroup 
)
explicit

Create a new window state saver for window.

Parameters
configGroupA KConfigGroup that holds the window state.

◆ KWindowStateSaver() [2/6]

KWindowStateSaver::KWindowStateSaver ( QWindow *  window,
const QString &  configGroupName 
)
explicit

Create a new window state saver for window.

Parameters
configGroupNameThe name of a KConfigGroup in the default state configuration (see KSharedConfig::openStateConfig) that holds the window state.

◆ KWindowStateSaver() [3/6]

KWindowStateSaver::KWindowStateSaver ( QWindow *  window,
const char *  configGroupName 
)
explicit

Create a new window state saver for window.

Parameters
configGroupNameThe name of a KConfigGroup in the default state configuration (see KSharedConfig::openStateConfig) that holds the window state.

◆ KWindowStateSaver() [4/6]

template<typename Widget >
KWindowStateSaver::KWindowStateSaver ( Widget *  widget,
const KConfigGroup configGroup 
)
inlineexplicit

Create a new window state saver for widget.

Use this for widgets that aren't shown yet and would still return nullptr from windowHandle().

Parameters
configGroupA KConfigGroup that holds the window state.

◆ KWindowStateSaver() [5/6]

template<typename Widget >
KWindowStateSaver::KWindowStateSaver ( Widget *  widget,
const QString &  configGroupName 
)
inlineexplicit

Create a new window state saver for widget.

Use this for widgets that aren't shown yet and would still return nullptr from windowHandle().

Parameters
configGroupNameThe name of a KConfigGroup in the default state configuration (see KSharedConfig::openStateConfig) that holds the window state.

◆ KWindowStateSaver() [6/6]

template<typename Widget >
KWindowStateSaver::KWindowStateSaver ( Widget *  widget,
const char *  configGroupName 
)
inlineexplicit

Create a new window state saver for widget.

Use this for widgets that aren't shown yet and would still return nullptr from windowHandle().

Parameters
configGroupNameThe name of a KConfigGroup in the default state configuration (see KSharedConfig::openStateConfig) that holds the window state.