KXMLGUIFactory Class
Header: | #include <KXMLGUIFactory> |
CMake: | find_package(KF6 REQUIRED COMPONENTS XmlGui) target_link_libraries(mytarget PRIVATE KF6::XmlGui) |
Inherits: | QObject |
Public Functions
KXMLGUIFactory(KXMLGUIBuilder *builder, QObject *parent = nullptr) | |
virtual | ~KXMLGUIFactory() override |
void | addClient(KXMLGUIClient *client) |
QList<KXMLGUIClient *> | clients() const |
QWidget * | container(const QString &containerName, KXMLGUIClient *client, bool useTagName = false) |
void | refreshActionProperties() |
void | removeClient(KXMLGUIClient *client) |
void | reset() |
void | resetContainer(const QString &containerName, bool useTagName = false) |
Public Slots
(since 5.84) void | showConfigureShortcutsDialog() |
Signals
(since 4.1.3) void | makingChanges(bool) |
(since 5.79) void | shortcutsSaved() |
Detailed Description
KXMLGUIFactory, together with KXMLGUIClient objects, can be used to create a GUI of container widgets (like menus, toolbars, etc.) and container items (menu items, toolbar buttons, etc.) from an XML document and action objects.
Each KXMLGUIClient represents a part of the GUI, composed from containers and actions. KXMLGUIFactory takes care of building (with the help of a KXMLGUIBuilder) and merging the GUI from an unlimited number of clients.
Each client provides XML through a QDomDocument and actions through a KActionCollection . The XML document contains the rules for how to merge the GUI.
KXMLGUIFactory processes the DOM tree provided by a client and plugs in the client's actions, according to the XML and the merging rules of previously inserted clients. Container widgets are built via a KXMLGUIBuilder , which has to be provided with the KXMLGUIFactory constructor.
Member Function Documentation
[explicit]
KXMLGUIFactory::KXMLGUIFactory(KXMLGUIBuilder *builder, QObject *parent = nullptr)
Constructs a KXMLGUIFactory.
The provided builder KXMLGUIBuilder with the given parent will be called for creating and removing container widgets when clients are added/removed from the GUI.
Note that the ownership of the given KXMLGUIBuilder object won't be transferred to this KXMLGUIFactory, so you have to take care of deleting it properly.
[override virtual noexcept]
KXMLGUIFactory::~KXMLGUIFactory()
Destructor.
void KXMLGUIFactory::addClient(KXMLGUIClient *client)
Creates the GUI described by the QDomDocument of the client, using the client's actions, and merges it with the previously created GUI.
This also means that the order in which clients are added to the factory is relevant; assuming that your application supports plugins, you should first add your application to the factory and then the plugin, so that the plugin's UI is merged into the UI of your application, and not the other way round.
QList<KXMLGUIClient *> KXMLGUIFactory::clients() const
Returns a list of all clients currently added to this factory.
QWidget *KXMLGUIFactory::container(const QString &containerName, KXMLGUIClient *client, bool useTagName = false)
Use this method to get access to a container widget with the name specified with containerName that is owned by the client.
The container name is specified with a "name" attribute in the XML document.
This function is particularly useful for getting hold of a popupmenu defined in an XMLUI file.
For instance:
QMenu *popup = static_cast<QMenu*>(guiFactory()->container("my_popup",this));
Where "my_popup" is the name of the menu in the XMLUI file, and "this" is XMLGUIClient which owns the popupmenu (e.g. the mainwindow, or the part, or the plugin...).
containerName The name of the container widget.
client Owner of the container widget.
useTagName Whether to compare the specified name with the name attribute or the tag name.
This method may return nullptr if no container with the given name exists or if the container is not owned by the client.
[signal, since 4.1.3]
void KXMLGUIFactory::makingChanges(bool)
Emitted when the factory is currently making changes to the GUI, i.e. adding or removing clients.
makingChanges(true) is emitted before any change happens, and makingChanges(false) is emitted after the change is done.
This allows e.g. KMainWindow to know that the GUI is being changed programmatically and not by the user (so there is no reason to save toolbar settings afterwards).
This function was introduced in 4.1.3.
void KXMLGUIFactory::refreshActionProperties()
Use this method to reset and reread action properties (shortcuts, etc.) for all actions.
This is needed, for example, when you change shortcuts scheme at runtime.
void KXMLGUIFactory::removeClient(KXMLGUIClient *client)
Removes the GUI described by the client by unplugging all provided actions and removing all owned containers (and storing container state information in the given client).
void KXMLGUIFactory::reset()
Use this method to free all memory allocated by the KXMLGUIFactory.
This deletes the internal node tree and therefore resets the internal state of the class. Please note that the actual GUI is NOT touched at all, meaning no containers are deleted nor any actions unplugged. That is something you have to do on your own. So use this method only if you know what you are doing :-)
Note: This will call KXMLGUIClient::setFactory(nullptr) for all inserted clients).
void KXMLGUIFactory::resetContainer(const QString &containerName, bool useTagName = false)
Free all memory allocated by the KXMLGUIFactory for a container named containerName, including all child containers and actions.
Additionally, you may set whether to compare the specified containerName with the name attribute or the tag name.
This deletes the internal node subtree for the specified container. The actual GUI is not touched, no containers are deleted or any actions unplugged.
Use this method only if you know what you are doing :-)
useTagName Whether to compare the specified name with the name attribute.
Note: This will call KXMLGUIClient::setFactory(nullptr) for all clients of the container).
[signal, since 5.79]
void KXMLGUIFactory::shortcutsSaved()
Emitted when the shortcuts have been saved (i.e. the user accepted the dialog).
If you're using multiple instances of the same KXMLGUIClient, you probably want to connect to this signal and call KXMLGUIClient::reloadXML()
for each of your KXMLGUIClients, so that the other instances update their shortcuts settings.
This function was introduced in 5.79.
[slot, since 5.84]
void KXMLGUIFactory::showConfigureShortcutsDialog()
Shows a dialog (KShortcutsDialog) that lists every action in this factory, and that can be used to change the shortcuts associated with each action.
This slot can be connected directly to the configure shortcuts action, for example:
KStandardAction::keyBindings(guiFactory(), &KXMLGUIFactory::showConfigureShortcutsDialog, actionCollection());
This method constructs a KShortcutsDialog with the default arguments (KShortcutsEditor::AllActions and KShortcutsEditor::LetterShortcutsAllowed).
By default the changes will be saved back to the *ui.rc
file they were initially read from.
If you need to run some extra code if the dialog is accepted and the settings are saved, you can simply connect to the KXMLGUIFactory::shortcutsSaved() signal before calling this method, for example:
connect(guiFactory(), &KXMLGUIFactory::shortcutsSaved, this, &MyClass::slotShortcutSaved); guiFactory()->showConfigureShortcutsDialog();
This function was introduced in 5.84.
See also KShortcutsDialog, KShortcutsEditor::ActionTypes, and KShortcutsEditor::LetterShortcuts.