Thanks. I managed to simulate selection using TAB thanks to this post solution.
Here is my code sample:
bool FacadeGraphicsView
::event( QEvent *event
) {
switch (event->type())
{
{
if (k->key() == Qt::Key_Backtab
|| (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))
|| (k->key() == Qt::Key_Tab) )
{
event->accept();
m_facadeScene()->manageTabSelection();
}
return true;
}
default:
}
}
CustomScene* FacadeGraphicsView::m_customScene()
{
auto pCustomScene = dynamic_cast<CustomScene*>(scene());
return pCustomScene;
}
bool FacadeGraphicsView::event( QEvent *event )
{
switch (event->type())
{
case QEvent::KeyPress:
{
QKeyEvent *k = (QKeyEvent *)event;
QGraphicsView::keyPressEvent(k);
if (k->key() == Qt::Key_Backtab
|| (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))
|| (k->key() == Qt::Key_Tab) )
{
event->accept();
m_facadeScene()->manageTabSelection();
}
return true;
}
default:
return QGraphicsView::event( event );
}
}
CustomScene* FacadeGraphicsView::m_customScene()
{
auto pCustomScene = dynamic_cast<CustomScene*>(scene());
return pCustomScene;
}
To copy to clipboard, switch view to plain text mode
void CustomScene::manageTabSelection()
{
if ((selectedItems().count() == 0) || (selectedItems().count() > 1))
{
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
current->setSelected(false);
}
}
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
current->setSelected(true);
break;
}
}
}
else
{
bool found = false;
// Find a selected item
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
if (current->isSelected())
{
current->setSelected(false);
found = true;
}
else
{
if (found)
{
current->setSelected(true);
break;
}
}
}
}
// if selected was the last item, ensure to select at least one item
if (selectedItems().count() == 0)
{
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
current->setSelected(true);
break;
}
}
}
}
}
void CustomScene::manageTabSelection()
{
if ((selectedItems().count() == 0) || (selectedItems().count() > 1))
{
foreach( QGraphicsItem *item, items() )
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
current->setSelected(false);
}
}
foreach( QGraphicsItem *item, items() )
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
current->setSelected(true);
break;
}
}
}
else
{
bool found = false;
// Find a selected item
foreach( QGraphicsItem *item, items() )
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
if (current->isSelected())
{
current->setSelected(false);
found = true;
}
else
{
if (found)
{
current->setSelected(true);
break;
}
}
}
}
// if selected was the last item, ensure to select at least one item
if (selectedItems().count() == 0)
{
foreach( QGraphicsItem *item, items() )
{
auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
if( current)
{
current->setSelected(true);
break;
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks