mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-15 00:02:52 -04:00
Add method to select a QgsDataItem directly in QgsBrowserTreeView
This commit is contained in:
parent
f57f9485d1
commit
f821fefdf2
@ -48,6 +48,15 @@ Returns the browser model
|
||||
|
||||
void setSettingsSection( const QString §ion );
|
||||
|
||||
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 );
|
||||
|
@ -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 )
|
||||
{
|
||||
|
@ -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 §ion ) { 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user