SUMMARY As the title says, when using the constructor that uses the name of the toolbar: KToggleToolBarAction (const char *toolBarName, const QString &text, QObject *parent) the name of the toolbar is just stored, but the toolbar itself is not retrieved, so the action is not working. See: https://api.kde.org/frameworks/kxmlgui/html/ktoggletoolbaraction_8cpp_source.html#l00057 SOFTWARE/OS VERSIONS Operating System: Manjaro Linux KDE Plasma Version: 5.15.5 KDE Frameworks Version: 5.59.0 Qt Version: 5.12.3 Kernel Version: 4.19.49-1-MANJARO OS Type: 64-bit
The documentation says "This can be either the name of a toolbar in an xml ui file, or a toolbar programmatically created with that name." I have no idea if it is really expected to be working with not-yet-created toobars. I guess it would have to globally watch all newly created objects to check if a toolbar with this name was created. If the toolbar already exists, I suggest to use the other constructor.
To be more clear, what I am trying to achieve is to add an action to toggle a toolbar created with XMLGUI. The problem is that I have no idea on how to retrieve the XMLGUI toolbar instance, in order to use the other constructor. I am not even sure that it is already created while I am trying to set up the actions. Any suggestions/reference on how to do this? I thought that the first constructor was intended to be used with XMLGUI, but apparently it is not. Looking at the code, I am not even sure that the first constructor will work with programmatically created named toolbars.
No, it doesn't work. As you wrote, it just stores the name, but never connects to any toolbar with that name. If the toolbar is already created, use QObject::findChild<KToolBar *>(name) on the window. I have to find a checkout of kdelibs4 to check if this is a porting regression, or if it never worked. As is, the code is useless.
I traced commits back to 2006. The 'toolBarName' feature never worked; there was no code to handle it. I am unsure if we should try to implement it (see difficulties described in comment #1), or just remove/deprecate the never working constructor.
In my situation I managed to workaround the problem as explained in the comments of https://phabricator.kde.org/D15580. The code would have been neater if I could have used constructuor 1: in this way I could have defined the action together with the other actions in pageview.cpp instead of defining it separately in shell.cpp. Probably deprecating the first constructor is the most straightforward solution. According to my superficial google searches KToggleToolBarAction does not seem to be used a lot, at least not many results popped up.