Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
Medical Imaging Interaction Toolkit
Qt Model/View selections

An example application plug-in with a minimal selection service based on Qt selection models.

In this example the selection service is used to connect the selection of the radio buttons from one view with the selection of the list of the other view. The SelectionView holds a QListWidget that provides the user selection (qt selection provider) for the selection listener (ListenerView). The radio buttons of the listener view are changed according to the selection in the QListWidget. Vice versa the radio buttons (the selection listener) does not provide any selection events. If the user changes the radio button state the QListWidget is not altered.

For additional information on the selection service concept see https://www.mitk.org/wiki/Article_Using_the_Selection_Service

The berry::QtSelectionProvider class implements the interface berry::ISelectionProvider. Due to the model/view concept in Qt, the workbench provides the berry::QtSelectionProvider class for Qt viewers which must be provided with a QItemSelectionModel.

In the SelectionView.h we declare a pointer that holds the selection provider...

...and in the SelectionView.cpp we set the selection model to the model of the QListWidget (m_SelectionList)

// create new qt selection provider
m_SelectionProvider = new berry::QtSelectionProvider();
// set the item selection model to the model of the QListWidget
m_SelectionProvider->SetItemSelectionModel(m_Controls->m_SelectionList->selectionModel());
// register selection provider
GetSite()->SetSelectionProvider(m_SelectionProvider);

Now that the QListWidget of the SelectionView sends out selection events we need a listener implementation. In the ListenerView.h we need to include the ISelectionListener which is a simple class with just one method.

The method that implements the selection listener functionality of ISelectionListener and the pointer that holds the selection listener is declared...

void SelectionChanged(const berry::IWorkbenchPart::Pointer &sourcepart,
QScopedPointer<berry::ISelectionListener> m_SelectionListener;

...and implemented in the cpp-file:

void ListenerView::SelectionChanged(const berry::IWorkbenchPart::Pointer &sourcepart,
{
// check for null selection
if (selection.IsNull())
{
return;
}
// exclude own selection events and check whether this kind of selection can be handled
if (sourcepart != this && selection.Cast<const berry::IStructuredSelection>())
{
// iterate over the selections (for the BlueBerry example this is always 1
for (berry::IStructuredSelection::iterator itr = currentSelection->Begin(); itr != currentSelection->End(); ++itr)
{
{
// get name of selected ListWidgetElement
QVariant text = object->GetQModelIndex().data();
// pass name of element to method for radio button state checking
ToggleRadioMethod(text.toString());
}
}
}
}

Now the name of the selected list element is passed to a method that toggles the radio buttons of the ListenerView accordingly.

View complete source files:

[Selection Service] [Next: MITK DataNode Selections] [BlueBerry Examples]

berry::IStructuredSelection
Definition: berryIStructuredSelection.h:28
berry::SmartPointer
Implements transparent reference counting.
Definition: berryICommandCategoryListener.h:21
berry::QModelIndexObject
Definition: berryQModelIndexObject.h:27
berry::IStructuredSelection::iterator
ContainerType::const_iterator iterator
Definition: berryIStructuredSelection.h:31
berry::QtSelectionProvider
Definition: berryQtSelectionProvider.h:27
berry::SmartPointer::IsNull
bool IsNull() const
Definition: berrySmartPointer.h:140
berry::SmartPointer::Cast
SmartPointer< Other > Cast() const
Definition: berrySmartPointer.h:111