QgsBrowserModel class

A model for showing available data sources and other items in a structured tree.

QgsBrowserModel is the foundation for the QGIS browser panel, and includes items for the different data providers and folders accessible to users.

QgsBrowserModel models are not initially populated and use a deferred initialization approach. After constructing a QgsBrowserModel, a call must be made to initialize() in order to populate the model.

Public types

enum ItemDataRole { PathRole = Qt::UserRole, CommentRole = Qt::UserRole + 1, SortRole }

Public static functions

static auto findPath(QAbstractItemModel* model, const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly) -> QModelIndex

Constructors, destructors, conversion operators

QgsBrowserModel(QObject* parent = nullptr) explicit
Constructor for QgsBrowserModel, with the specified parent object.

Public functions

auto connectItem(QgsDataItem* item) -> Q_DECL_DEPRECATED void deprecated
auto dataItem(const QModelIndex& idx) const -> QgsDataItem*
Returns the data item at the specified index, or a nullptr if no item exists at the index.
auto driveItems() const -> QMap<QString, QgsDirectoryItem*>
Returns a map of the root drive items shown in the browser.
auto findItem(QgsDataItem* item, QgsDataItem* parent = nullptr) const -> QModelIndex
Returns the model index corresponding to the specified data item.
auto findPath(const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly) -> QModelIndex
Returns index of item with given path.
auto findUri(const QString& uri, QModelIndex index = QModelIndex()) -> QModelIndex
Returns index of layer item with given uri.
auto initialized() const -> bool
Returns true if the model has been initialized.
void refresh(const QString& path)
Refresh item specified by path.
void refresh(const QModelIndex& index = QModelIndex())
Refresh item children.

Signals

void connectionsChanged()
Connections changed in the browser, forwarded to the widget and used to notify the provider dialogs of a changed connection.
void stateChanged(const QModelIndex& index, QgsDataItem::State oldState)
Emitted when item children fetch was finished.

Public slots

void addFavoriteDirectory(const QString& directory, const QString& name = QString())
Adds a directory to the favorites group.
void hidePath(QgsDataItem* item)
Hide the given path in the browser model.
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
void refreshDrives()
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly added drives.
void reload()
Reload the whole model.
void removeFavorite(const QModelIndex& index)
Removes a favorite directory from its corresponding model index.
void removeFavorite(QgsFavoriteItem* favorite)
Removes a favorite item.

Protected functions

void addRootItems()
Populates the model.

Enum documentation

enum QgsBrowserModel::ItemDataRole

Enumerators
PathRole

Item path used to access path in the tree, see QgsDataItem::mPath.

CommentRole

Item comment.

SortRole

Custom sort role, see QgsDataItem::sortKey()

Function documentation

static QModelIndex QgsBrowserModel::findPath(QAbstractItemModel* model, const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly)

QgsBrowserModel::QgsBrowserModel(QObject* parent = nullptr) explicit

Constructor for QgsBrowserModel, with the specified parent object.

Q_DECL_DEPRECATED void QgsBrowserModel::connectItem(QgsDataItem* item)

Any usage should be removed (and will have no harmful side-effects!).

QMap<QString, QgsDirectoryItem*> QgsBrowserModel::driveItems() const

Returns a map of the root drive items shown in the browser.

These correspond to the top-level directory items shown, e.g. on Windows the C:\, D:\, etc, and on Linux the "/" root directory.

QModelIndex QgsBrowserModel::findItem(QgsDataItem* item, QgsDataItem* parent = nullptr) const

Returns the model index corresponding to the specified data item.

If the item was not found, an invalid QModelIndex is returned.

If the parent item is argument is specified, then only items which are children of parent are searched. If no parent is specified, then all items within the model are searched.

QModelIndex QgsBrowserModel::findPath(const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly)

Returns index of item with given path.

Parameters
path item path
matchFlag supported is Qt::MatchExactly and Qt::MatchStartsWith which has reverse meaning, i.e. find item with the longest match from start with path (to get as close/deep as possible to deleted item).
Returns model index, invalid if item not found

It only searches in currently fetched items, i.e. it does not fetch children.

QModelIndex QgsBrowserModel::findUri(const QString& uri, QModelIndex index = QModelIndex())

Returns index of layer item with given uri.

Parameters
uri item uri
index the current index of the parent (to search for children)
Returns model index, invalid if item not found

It only searches in currently fetched items, i.e. it does not fetch children.

bool QgsBrowserModel::initialized() const

Returns true if the model has been initialized.

void QgsBrowserModel::addFavoriteDirectory(const QString& directory, const QString& name = QString()) public slot

Adds a directory to the favorites group.

If name is specified, it will be used as the favorite's name. Otherwise the name will be set to match directory.

void QgsBrowserModel::initialize() public slot

Delayed initialization, needed because the provider registry must be already populated.

void QgsBrowserModel::refreshDrives() public slot

Refreshes the list of drive items, removing any corresponding to removed drives and adding newly added drives.

void QgsBrowserModel::removeFavorite(const QModelIndex& index) public slot

Removes a favorite directory from its corresponding model index.

void QgsBrowserModel::removeFavorite(QgsFavoriteItem* favorite) public slot

Removes a favorite item.