QGraphicsScene: select multiple items
-
Hi everybody!
In last couple hours i can't figure out how implement multiselection for GraphicsScene items without Ctrl key.
What i tried is reimplement mousePressEvent of QGraphicsScene class:
@void SelectableGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent event)
{
QGraphicsItem ItemUnderMouse = itemAt(event->scenePos().x(), event->scenePos().y(), QTransform());
if (!ItemUnderMouse)
return;
if (ItemUnderMouse->isEnabled() && ItemUnderMouse->flags() & QGraphicsItem::ItemIsSelectable)
{
bool isSelected = ItemUnderMouse->isSelected();
ItemUnderMouse->setSelected(!isSelected);
}//QGraphicsScene::mousePressEvent(event);
}@
Multiselection works but i can't drag items any more. If i uncomment QGraphicsScene::mousePressEvent(event); dragging works but as expected multiselection stops work. I think there must be a easy way to solve this. -
Following solution works:
@void MemberGraphicsItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
event->setModifiers(event->modifiers() | Qt::ControlModifier);
QGraphicsItem::mousePressEvent(event);
}void MemberGraphicsItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
event->setModifiers(event->modifiers() | Qt::ControlModifier);
QGraphicsItem::mouseReleaseEvent(event);
}@
but i am still not sure, is it right solution. -
try
@void MemberGraphicsItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
if(!isSelected())
setSelected(true);
else
setSelected(false);
}@... don't call the base class QGraphicsItem::mouseReleaseEvent handler
-
If you look at QGraphicsItem source code:
mousePressEvent
@void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable)) {
bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
if (!multiSelect) {
if (!d_ptr->selected) {
if (QGraphicsScene *scene = d_ptr->scene) {
++scene->d_func()->selectionChanging;
scene->clearSelection();
--scene->d_func()->selectionChanging;
}
setSelected(true);
}
}
} else if (!(flags() & ItemIsMovable)) {
event->ignore();
}
if (d_ptr->isWidget) {
// Qt::Popup closes when you click outside.
QGraphicsWidget *w = static_cast<QGraphicsWidget *>(this);
if ((w->windowFlags() & Qt::Popup) == Qt::Popup) {
event->accept();
if (!w->rect().contains(event->pos()))
w->close();
}
}
}@mouseReleaseEvent
@void QGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (flags() & ItemIsSelectable) {
bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
if (event->scenePos() == event->buttonDownScenePos(Qt::LeftButton)) {
// The item didn't move
if (multiSelect) {
setSelected(!isSelected());
} else {
bool selectionChanged = false;
if (QGraphicsScene *scene = d_ptr->scene) {
++scene->d_func()->selectionChanging;
// Clear everything but this item. Bypass
// QGraphicsScene::clearSelection()'s default behavior by
// temporarily removing this item from the selection list.
if (d_ptr->selected) {
scene->d_func()->selectedItems.remove(this);
foreach (QGraphicsItem *item, scene->d_func()->selectedItems) {
if (item->isSelected()) {
selectionChanged = true;
break;
}
}
}
scene->clearSelection();
if (d_ptr->selected)
scene->d_func()->selectedItems.insert(this);
--scene->d_func()->selectionChanging;
if (selectionChanged)
emit d_ptr->scene->selectionChanged();
}
setSelected(true);
}
}
}
if (d_ptr->scene && !event->buttons())
d_ptr->scene->d_func()->movingItemsInitialPositions.clear();
}@impossible do this without modifing bouth mousePressEvent and mouseReleaseEvent. Also dragging not works properly without:
@if (d_ptr->scene && !event->buttons())
d_ptr->scene->d_func()->movingItemsInitialPositions.clear();@
in mouseReleaseEvent. However you can't just add these lines to overloaded function (error: reference to 'd_ptr' is ambiguous). -
You could do something very bad, and modify the event's keyboard modifier, then hand the event over to the base class.
Otherwise, it sounds like it's best to implement both selection and move completely in your own class, and no longer call base class events.