KXmlGuiWindow Class

KMainWindow with convenience functions and integration with XmlGui files. More...

Header: #include <KXmlGuiWindow>
CMake: find_package(KF6 REQUIRED COMPONENTS XmlGui)
target_link_libraries(mytarget PRIVATE KF6::XmlGui)
Inherits: KMainWindow, KXMLGUIBuilder, and KXMLGUIClient

Public Types

enum StandardWindowOption { ToolBar, Keys, StatusBar, Save, Create, Default }
flags StandardWindowOptions

Properties

Public Functions

KXmlGuiWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
virtual ~KXmlGuiWindow() override
void createGUI(const QString &xmlfile = QString())
void createStandardStatusBarAction()
virtual KXMLGUIFactory *guiFactory()
(since 5.83) bool isCommandBarEnabled() const
bool isHelpMenuEnabled() const
bool isStandardToolBarMenuEnabled() const
(since 5.83) void setCommandBarEnabled(bool showCommandBar)
void setHelpMenuEnabled(bool showHelpMenu = true)
void setStandardToolBarMenuEnabled(bool showToolBarMenu)
void setupGUI(KXmlGuiWindow::StandardWindowOptions options = Default, const QString &xmlfile = QString())
void setupGUI(const QSize &defaultSize, KXmlGuiWindow::StandardWindowOptions options = Default, const QString &xmlfile = QString())
QAction *toolBarMenuAction()

Public Slots

virtual void configureToolbars()
virtual void slotStateChanged(const QString &newstate)
void slotStateChanged(const QString &newstate, bool reverse)

Protected Functions

(since 5.30) void checkAmbiguousShortcuts()

Reimplemented Protected Functions

virtual bool event(QEvent *event) override

Protected Slots

virtual void saveNewToolbarConfig()

Detailed Description

This class includes several convenience <action>Enabled() functions to toggle the presence of functionality in your main window, including a KCommandBar instance.

The StandardWindowOptions enum can be used to pass additional options to describe the main window behavior/appearance. Use it in conjunction with setupGUI() to load an appnameui.rc file to manage the main window's actions.

setCommandBarEnabled() is set by default.

A minimal example can be created with QMainWindow::setCentralWidget() and setupGUI():

MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent) {
  textArea = new KTextEdit();
  setCentralWidget(textArea);
  setupGUI(Default);
}

With this, a ready-made main window with menubar and statusbar is created, as well as two default menus, Settings and Help.

Management of QActions is made trivial in combination with KActionCollection and KStandardAction.

void MainWindow::setupActions() {
  QAction *clearAction = new QAction(this);
  clearAction->setText(i18n("&Clear"));
  clearAction->setIcon(QIcon::fromTheme("document-new"));
  KActionCollection::setDefaultShortcut(clearAction, Qt::CTRL + Qt::Key_W);
  actionCollection()->addAction("clear", clearAction);
  connect(clearAction, &QAction::triggered, textArea, &KTextEdit::clear);
  KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());
  setupGUI(Default, "texteditorui.rc");
}

See https://develop.kde.org/docs/use/kxmlgui/ for a tutorial on how to create a simple text editor using KXmlGuiWindow.

See https://develop.kde.org/docs/use/session-managment for more information on session management.

See also KMainWindow, KActionCollection, KStandardAction, setupGUI(), createGUI(), and setCommandBarEnabled().

Member Type Documentation

enum KXmlGuiWindow::StandardWindowOption
flags KXmlGuiWindow::StandardWindowOptions

Use these options for the first argument of setupGUI().

ConstantValueDescription
KXmlGuiWindow::ToolBar1Adds action(s) to show/hide the toolbar(s) and adds a menu action to configure the toolbar(s).
KXmlGuiWindow::Keys2Adds an action in the 'Settings' menu to open the configure keyboard shortcuts dialog.
KXmlGuiWindow::StatusBar4Adds an action to show/hide the statusbar in the 'Settings' menu. Note that setting this value will create a statusbar if one doesn't already exist.
KXmlGuiWindow::Save8Autosaves (and loads) the toolbar/menubar/statusbar settings and window size using the default name. Like KMainWindow::setAutoSaveSettings(), enabling this causes the application to save state data upon close in a KConfig-managed configuration file. Typically you want to let the default window size be determined by the widgets' size hints. Make sure that setupGUI() is called after all the widgets are created (including QMainWindow::setCentralWidget()) so that the default size is managed properly.
KXmlGuiWindow::Create16Calls createGUI() once ToolBar, Keys and Statusbar have been taken care of.
KXmlGuiWindow::DefaultToolBar | Keys | StatusBar | Save | CreateSets all of the above options as true.

Note: In the case of KXmlGuiWindow::Create, when using KParts::MainWindow, remove this flag from the setupGUI() call, since you'll be using createGUI(part) instead:

setupGUI(ToolBar | Keys | StatusBar | Save);

The StandardWindowOptions type is a typedef for QFlags<StandardWindowOption>. It stores an OR combination of StandardWindowOption values.

See also setupGUI(), setStandardToolBarMenuEnabled(), isStandardToolBarMenuEnabled(), createStandardStatusBarAction(), KMainWindow::setAutoSaveSettings(), KConfig, and createGUI().

Property Documentation

[read-only] autoSaveGroup : const QString

Access functions:

QString autoSaveGroup() const

[read-only] autoSaveSettings : const bool

Access functions:

bool autoSaveSettings() const

[read-only] hasMenuBar : const bool

Access functions:

bool hasMenuBar()

standardToolBarMenuEnabled : bool

Access functions:

bool isStandardToolBarMenuEnabled() const
void setStandardToolBarMenuEnabled(bool showToolBarMenu)

Member Function Documentation

[explicit] KXmlGuiWindow::KXmlGuiWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())

Construct a main window.

Note that by default a KXmlGuiWindow is created with the Qt::WA_DeleteOnClose attribute set, i.e. it is automatically destroyed when the window is closed. If you do not want this behavior, call:

window->setAttribute(Qt::WA_DeleteOnClose, false);

KXmlGuiWindows must be created on the heap with 'new', like:

KXmlGuiWindow *kmw = new KXmlGuiWindow(...);
kmw->setObjectName(...);

Note: For session management and window management to work properly, all main windows in the application should have a different name. Otherwise, the base class KMainWindow will create a unique name, but it's recommended to explicitly pass a window name that will also describe the type of the window. If there can be several windows of the same type, append '#' (hash) to the name, and KMainWindow will replace it with numbers to make the names unique. For example, for a mail client which has one main window showing the mails and folders, and which can also have one or more windows for composing mails, the name for the folders window should be e.g. "mainwindow" and for the composer windows "composer#".

parent The widget parent. This is usually nullptr, but it may also be the window group leader. In that case, the KXmlGuiWindow becomes a secondary window.

flags Specify the window flags. The default is none.

See also KMainWindow::KMainWindow.

[override virtual noexcept] KXmlGuiWindow::~KXmlGuiWindow()

Destructor.

Will also destroy the toolbars and menubar if needed.

[protected, since 5.30] void KXmlGuiWindow::checkAmbiguousShortcuts()

Checks if there are actions using the same shortcut.

This is called automatically from createGUI().

This function was introduced in 5.30.

[virtual slot] void KXmlGuiWindow::configureToolbars()

Show a standard configure toolbar dialog.

This slot can be connected directly to the action to configure the toolbar.

Example code:

KStandardAction::configureToolbars(this, &KXmlGuiWindow::configureToolbars, actionCollection);

void KXmlGuiWindow::createGUI(const QString &xmlfile = QString())

Generates the interface based on a local XML file.

This is the function that generates UI elements such as the main menu, toolbar (if any) and statusbar. This is called by setupGUI(Create) as well.

Typically, in a regular application, you would use setupGUI() instead, as it sets up the toolbar/shortcut edit actions, among other things.

If xmlfile is an empty string, this method will try to construct a local XML filename like appnameui.rc where 'appname' is your app's name. Typically that app name is what KXMLGUIClient::componentName() returns. If that file does not exist, then the XML UI code will use only the global (standard) XML file for its layout purposes.

xmlfile The path (relative or absolute) to the local xmlfile.

See also setupGUI().

void KXmlGuiWindow::createStandardStatusBarAction()

Creates a toggle under the 'Settings' menu to show/hide the statusbar.

Calling this method will create a statusbar if one doesn't already exist.

If an application maintains the action on its own (i.e. never calls this function), a connection needs to be made to let KMainWindow know when the hidden/shown status of the statusbar has changed. For example:

connect(action, &QAction::triggered,
        kmainwindow, &KMainWindow::setSettingsDirty);

Otherwise the status might not be saved by KMainWindow.

Note: This function only makes sense before calling createGUI() or when using setupGUI() without StandardWindowOption::StatusBar.

See also createGUI(), setupGUI(), StandardWindowOption, KStandardAction::showStatusbar(), setStandardToolBarMenuEnabled(), QMainWindow::setStatusBar(), and QMainWindow::statusBar().

[override virtual protected] bool KXmlGuiWindow::event(QEvent *event)

Reimplements: KMainWindow::event(QEvent *event).

Reimplemented to return the event QEvent::Polish in order to adjust the object name if needed, once all constructor code for the main window has run.

Also reimplemented to catch when a QDockWidget is added or removed.

[virtual] KXMLGUIFactory *KXmlGuiWindow::guiFactory()

[since 5.83] bool KXmlGuiWindow::isCommandBarEnabled() const

Whether a KCommandBar was set.

Returns true by default, false if setCommandBarEnabled(false) was set.

This function was introduced in 5.83.

See also setCommandBarEnabled().

bool KXmlGuiWindow::isHelpMenuEnabled() const

Returns true if the help menu is enabled, false if setHelpMenuEnabled(false) was set.

See also setHelpMenuEnabled().

bool KXmlGuiWindow::isStandardToolBarMenuEnabled() const

Returns whether setStandardToolBarMenuEnabled() was set.

Note: This function only makes sense if createGUI() was used. This function returns true only if setStandardToolBarMenuEnabled() was set and will return false even if StandardWindowOption::ToolBar was used.

Returns true if setStandardToolBarMenuEnabled() was set, false otherwise.

Note: Getter function for property standardToolBarMenuEnabled.

See also createGUI(), setupGUI(), setStandardToolBarMenuEnabled(), and StandardWindowOption.

[virtual protected slot] void KXmlGuiWindow::saveNewToolbarConfig()

Rebuilds the GUI after KEditToolBar changes the toolbar layout.

See also configureToolbars().

[since 5.83] void KXmlGuiWindow::setCommandBarEnabled(bool showCommandBar)

Enable a KCommandBar to list and quickly execute actions.

A KXmlGuiWindow by default automatically creates a KCommandBar, but it is inaccessible unless createGUI() or setupGUI(Create) is used.

It provides a HUD-like menu that lists all QActions in your application and can be activated via Ctrl+Atl+i or via an action in the 'Help' menu.

If you need more than a global set of QActions listed for your application, use KCommandBar directly instead.

showCommandBar Whether to show the command bar. true by default.

This function was introduced in 5.83.

See also KCommandBar, KCommandBar::setActions(), and isCommandBarEnabled().

void KXmlGuiWindow::setHelpMenuEnabled(bool showHelpMenu = true)

Creates a standard help menu when calling createGUI() or setupGUI().

showHelpMenu Whether to create a Help Menu. true by default.

See also isHelpMenuEnabled().

void KXmlGuiWindow::setStandardToolBarMenuEnabled(bool showToolBarMenu)

Creates a toggle under the 'Settings' menu to show/hide the available toolbars.

The standard toolbar menu toggles the visibility of one or multiple toolbars.

If there is only one toolbar configured, a simple 'Show <toolbar name>' menu item is shown; if more than one toolbar is configured, a "Shown Toolbars" menu is created instead, with 'Show <toolbar1 name>', 'Show <toolbar2 name>' ... sub-menu actions.

If your application uses a non-default XmlGui resource file, then you can specify the exact position of the menu/menu item by adding a <Merge name="StandardToolBarMenuHandler"> line to the settings menu section of your resource file ( usually appname.rc ).

showToolBarMenu Whether to show the standard toolbar menu. false by default.

Note: This function only makes sense before calling createGUI(). Using setupGUI(ToolBar) overrides this function.

Note: Setter function for property standardToolBarMenuEnabled.

See also isStandardToolBarMenuEnabled(), createGUI(), setupGUI(), KToggleBarAction, StandardWindowOption, KMainWindow::toolBar(), KMainWindow::toolBars(), QMainWindow::addToolBar(), QMainWindow::removeToolBar(), and createStandardStatusBarAction().

void KXmlGuiWindow::setupGUI(KXmlGuiWindow::StandardWindowOptions options = Default, const QString &xmlfile = QString())

Configures the current window and its actions in the typical KDE fashion.

You can specify which window options/features are going to be set up using options, see the StandardWindowOptions enum for more details.

MainWindow::MainWindow(QWidget* parent) : KXmlGuiWindow(parent){
  textArea = new KTextEdit();
  setCentralWidget(textArea);
  setupGUI(Default, "appnameui.rc");
}

Use a bitwise OR (|) to select multiple enum choices for setupGUI() (except when using StandardWindowOptions::Default).

setupGUI(Save | Create, "appnameui.rc");

Typically this function replaces createGUI(), but it is possible to call setupGUI(Create) together with helper functions such as setStandardToolBarMenuEnabled() and createStandardStatusBarAction().

Warning: To use createGUI() and setupGUI() for the same window, you must avoid using StandardWindowOption::Create. Prefer using only setupGUI().

Note: When StandardWindowOption::Save is used, this method will restore the state of the application window (toolbar, dockwindows positions ...etc), so you need to have added all your actions to your UI before calling this method.

options A combination of StandardWindowOptions to specify UI elements to be present in your application window.

xmlfile The relative or absolute path to the local xmlfile. If this is an empty string, the code will look for a local XML file appnameui.rc, where 'appname' is the name of your app. See the note about the xmlfile argument in createGUI().

See also StandardWindowOption.

void KXmlGuiWindow::setupGUI(const QSize &defaultSize, KXmlGuiWindow::StandardWindowOptions options = Default, const QString &xmlfile = QString())

This is an overloaded function.

defaultSize A manually specified window size that overrides the saved size.

options A combination of StandardWindowOptions to specify UI elements to be present in your application window.

xmlfile The relative or absolute path to the local xmlfile.

See also setupGUI().

[virtual slot] void KXmlGuiWindow::slotStateChanged(const QString &newstate)

Applies a state change

Reimplement this to enable and disable actions as defined in the XmlGui rc file.

newstate The state change to be applied.

[slot] void KXmlGuiWindow::slotStateChanged(const QString &newstate, bool reverse)

Applies a state change

Reimplement this to enable and disable actions as defined in the XmlGui rc file.

This function can "reverse" the state (disable the actions which should be enabled, and vice-versa) if specified.

newstate The state change to be applied.

reverse Whether to reverse newstate or not.

QAction *KXmlGuiWindow::toolBarMenuAction()

Returns a pointer to the main window's action responsible for the toolbar's menu.