Add method to select a QgsDataItem directly in QgsBrowserTreeView

This commit is contained in:
Nyall Dawson 2022-07-14 14:16:45 +10:00
parent f57f9485d1
commit f821fefdf2
3 changed files with 38 additions and 0 deletions

View File

@ -48,6 +48,15 @@ Returns the browser model
void setSettingsSection( const QString &section );
bool setSelectedItem( QgsDataItem *item );
%Docstring
Sets the ``item`` currently selected in the view.
Returns ``True`` if the item was found and could be selected.
.. versionadded:: 3.28
%End
protected:
virtual void keyPressEvent( QKeyEvent *event );

View File

@ -21,6 +21,7 @@
#include "qgsdataitem.h"
#include <QKeyEvent>
#include <QSortFilterProxyModel>
QgsBrowserTreeView::QgsBrowserTreeView( QWidget *parent )
: QTreeView( parent )
@ -170,6 +171,24 @@ bool QgsBrowserTreeView::hasExpandedDescendant( const QModelIndex &index ) const
return false;
}
bool QgsBrowserTreeView::setSelectedItem( QgsDataItem *item )
{
if ( !mBrowserModel )
return false;
QModelIndex index = mBrowserModel->findItem( item );
if ( !index.isValid() )
return false;
if ( QSortFilterProxyModel *proxyModel = qobject_cast< QSortFilterProxyModel *>( model() ) )
{
index = proxyModel->mapFromSource( index );
}
setCurrentIndex( index );
return true;
}
// rowsInserted signal is used to continue in state restoring
void QgsBrowserTreeView::rowsInserted( const QModelIndex &parentIndex, int start, int end )
{

View File

@ -21,6 +21,7 @@
#include "qgis_gui.h"
class QgsBrowserGuiModel;
class QgsDataItem;
/**
* \ingroup gui
@ -51,6 +52,15 @@ class GUI_EXPORT QgsBrowserTreeView : public QTreeView
// Set section where to store settings (because we have 2 browser dock widgets)
void setSettingsSection( const QString &section ) { mSettingsSection = section; }
/**
* Sets the \a item currently selected in the view.
*
* Returns TRUE if the item was found and could be selected.
*
* \since QGIS 3.28
*/
bool setSelectedItem( QgsDataItem *item );
protected:
void keyPressEvent( QKeyEvent *event ) override;