KLinkItemSelectionModel Class

Makes it possible to share a selection in multiple views which do not have the same source model. More...

Header: #include <KLinkItemSelectionModel>
CMake: find_package(KF6 REQUIRED COMPONENTS ItemModels)
target_link_libraries(mytarget PRIVATE KF6::ItemModels)
Since: KDE Frameworks 4.5

Public Functions

KLinkItemSelectionModel(QAbstractItemModel *targetModel, QItemSelectionModel *linkedItemSelectionModel, QObject *parent = nullptr)
QItemSelectionModel *linkedItemSelectionModel() const
void setLinkedItemSelectionModel(QItemSelectionModel *selectionModel)

Signals

Detailed Description

Although multiple views can share the same QItemSelectionModel, the views then need to have the same source model.

If there is a proxy model between the model and one of the views, or different proxy models in each, this class makes it possible to share the selection between the views.

"Sharing a QItemSelectionModel between views on the same model is trivial"

"If a proxy model is used, it is no longer possible to share the QItemSelectionModel directly"

"A KLinkItemSelectionModel can be used to map the selection through the proxy model"

QAbstractItemModel *model = getModel();

QSortFilterProxyModel *proxy = new QSortFilterProxyModel();
proxy->setSourceModel(model);

QTreeView *view1 = new QTreeView(splitter);
view1->setModel(model);

KLinkItemSelectionModel *view2SelectionModel = new KLinkItemSelectionModel( proxy, view1->selectionModel());

QTreeView *view2 = new QTreeView(splitter);
// Note that the QAbstractItemModel passed to KLinkItemSelectionModel must be the same as what is used in the view
view2->setModel(proxy);
view2->setSelectionModel( view2SelectionModel );

"Arbitrarily complex proxy configurations on the same root model can be used"

QAbstractItemModel *model = getModel();

QSortFilterProxyModel *proxy1 = new QSortFilterProxyModel();
proxy1->setSourceModel(model);
QSortFilterProxyModel *proxy2 = new QSortFilterProxyModel();
proxy2->setSourceModel(proxy1);
QSortFilterProxyModel *proxy3 = new QSortFilterProxyModel();
proxy3->setSourceModel(proxy2);

QTreeView *view1 = new QTreeView(splitter);
view1->setModel(proxy3);

QSortFilterProxyModel *proxy4 = new QSortFilterProxyModel();
proxy4->setSourceModel(model);
QSortFilterProxyModel *proxy5 = new QSortFilterProxyModel();
proxy5->setSourceModel(proxy4);

KLinkItemSelectionModel *view2SelectionModel = new KLinkItemSelectionModel( proxy5, view1->selectionModel());

QTreeView *view2 = new QTreeView(splitter);
// Note that the QAbstractItemModel passed to KLinkItemSelectionModel must be the same as what is used in the view
view2->setModel(proxy5);
view2->setSelectionModel( view2SelectionModel );

See also kitemmodels: tests/proxymodeltestapp/proxyitemselectionwidget.cpp.

@author Stephen Kelly <steveire@gmail.com>

Member Function Documentation

KLinkItemSelectionModel::KLinkItemSelectionModel(QAbstractItemModel *targetModel, QItemSelectionModel *linkedItemSelectionModel, QObject *parent = nullptr)

Constructor.