KWindowStateSaver Class

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

Header: #include <KWindowStateSaver>
CMake: find_package(KF6 REQUIRED COMPONENTS Config)
target_link_libraries(mytarget PRIVATE KF6::ConfigGui)
Since: 5.92
Inherits: QObject

Public Functions

KWindowStateSaver(QWindow *window, const KConfigGroup &configGroup)
KWindowStateSaver(QWindow *window, const QString &configGroupName)
KWindowStateSaver(Widget *widget, const KConfigGroup &configGroup)
KWindowStateSaver(Widget *widget, const QString &configGroupName)

Detailed Description

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();

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.

Member Function Documentation

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

Create a new window state saver for window.

configGroup A KConfigGroup that holds the window state.

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

Create a new window state saver for window.

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

[explicit] template <typename Widget> KWindowStateSaver::KWindowStateSaver(Widget *widget, const KConfigGroup &configGroup)

Create a new window state saver for widget.

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

configGroup A KConfigGroup that holds the window state.

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

Create a new window state saver for widget.

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

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