Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ddbf5ad

Browse files
nyalldawsongithub-actions[bot]
authored andcommittedMar 15, 2023
Add a "contextMenuAboutToShow" signal to QgsLayerTreeView
Allows plugins to hook into this menu and add custom entries and customise the standard entries
1 parent 9108e73 commit ddbf5ad

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed
 

‎python/gui/auto_generated/layertree/qgslayertreeview.sip.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ Emitted when a current layer is changed
357357
void datasetsDropped( QDropEvent *event );
358358
%Docstring
359359
Emitted when datasets are dropped onto the layer tree view
360+
%End
361+
362+
void contextMenuAboutToShow( QMenu *menu );
363+
%Docstring
364+
Emitted when the context menu is about to show.
365+
366+
Allows customisation of the menu.
367+
368+
.. versionadded:: 3.32
360369
%End
361370

362371
protected:

‎src/gui/layertree/qgslayertreeview.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,14 @@ void QgsLayerTreeView::contextMenuEvent( QContextMenuEvent *event )
179179
setCurrentIndex( QModelIndex() );
180180

181181
QMenu *menu = mMenuProvider->createContextMenu();
182-
if ( menu && menu->actions().count() != 0 )
183-
menu->exec( mapToGlobal( event->pos() ) );
184-
delete menu;
182+
if ( menu )
183+
{
184+
emit contextMenuAboutToShow( menu );
185+
186+
if ( menu->actions().count() != 0 )
187+
menu->exec( mapToGlobal( event->pos() ) );
188+
delete menu;
189+
}
185190
}
186191

187192

‎src/gui/layertree/qgslayertreeview.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
354354
//! Emitted when datasets are dropped onto the layer tree view
355355
void datasetsDropped( QDropEvent *event );
356356

357+
/**
358+
* Emitted when the context menu is about to show.
359+
*
360+
* Allows customisation of the menu.
361+
*
362+
* \since QGIS 3.32
363+
*/
364+
void contextMenuAboutToShow( QMenu *menu );
365+
357366
protected:
358367
void contextMenuEvent( QContextMenuEvent *event ) override;
359368

0 commit comments

Comments
 (0)
Please sign in to comment.