1
QgsVectorLayer* vLyr=static_cast<QgsVectorLayer*>(aktLayer->qgsLayer);
vLyr->actions()->addAction(QgsAction::ActionType::OpenUrl, "Link im Webbrowser öffnen", "[% "+sAttribute+" %]" , ":/images/images/hotlink.png", false);
vLyr->actions()->setDefaultAction(vLyr->actions()->size()-1);

In my plugin after loading a layer to mapregistry i add with the code above an attribute-action to the layer. But i can't use it, because the button in attributes-toolbar is disabled. Looking at the layerproperties-dialog all seems to be ok and only after clicking 'OK' (not 'Apply') in the dialog the button in toolbar gets enabled and all works fine. What function do I have to call to reload/refresh the attributes-toolbar in QGIS? Does anybody know this?

EDIT:

found the method "void QgisApp::refreshFeatureActions()" which maybe could solve my problem but how can i access this class from my cpp-plugin?

EDIT:

The final solution for me is the following:

QgsVectorLayer* vLyr=static_cast<QgsVectorLayer*>(aktLayer->qgsLayer);

QToolBar* attributeToolbar= mQGisIface->mainWindow()->findChild<QToolBar*>("mAttributesToolBar");

vLyr->actions()->addAction(QgsAction::ActionType::OpenUrl, "Link im Webbrowser öffnen", "[% "+sAttribute+" %]" , ":/images/images/hotlink.png", false);
vLyr->actions()->setDefaultAction(vLyr->actions()->size()-1);

QgsMapLayerAction * mapAction=new QgsMapLayerAction("Link im Webbrowser öffnen", attributeToolbar, aktLayer->qgsLayer, QgsMapLayerAction::Target::AllActions);//, new QIcon(":/images/images/hotlink.png"));
QgsMapLayerActionRegistry::instance()->addMapLayerAction(mapAction);
QgsMapLayerActionRegistry::instance()->removeMapLayerAction(mapAction);

I have to remove the QgsMapLayerAction because it doesn't know that it is an OpenURL an so it is empty. But only adding an entry to the QgsMapLayerActionRegistry emits a signal to check the toolbar availibility. I don't know why I have no access to QgisApp-class, can't include qgisapp.h.

1
+50

I am not sure if this help you since this is for python. Why not add the action to attributes toolBar.

attributeToolBar = self.iface.mainWindow().findChild(QToolBar,'mattributeToolBar')

action = QtGui.QAction(parent)

attributeToolBar.addAction(action)
  • 1
    Thank you so much. You gave me the right hint. But was little bit confusing because QGis needs to be tricked a little bit. First I get the toolbar analog to your code: QToolBar* attributeToolbar= mQGisIface->mainWindow()->findChild<QToolBar*>("mAttributesToolBar"); But I'll have to create a QgsMapLayerAction to add the toolbar, then qgis refreshes the toolbar, but shoes me 2 actions, one of them is empty, so Ill have to remove the MapLayerAction and it works fine. – Ms. Smith Apr 17 '15 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.