My bad, I was actually using QGraphicsView as a base. I've instead made a custom QGraphicsScene and I've overridden the mouse press event, like so:
void CustomGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *evt) {
switch (evt->button()) {
case Qt::LeftButton: {
QList<QGraphicsItem *> items =
this->items(evt->pos(), Qt::IntersectsItemBoundingRect);
QListIterator<QGraphicsItem *> it(items);
while (it.hasNext()) {
QGraphicsItem *item = it.next();
if (item->boundingRegion(item->sceneTransform())
.contains(evt->pos().toPoint())) {
item->setSelected(!item->isSelected());
break; // This can happen twice cuz of float rounding nonsense
}
}
break;
}
case Qt::RightButton: {
clearSelection();
break;
}
default: {
break;
}
}
QGraphicsScene::mousePressEvent(evt);
}
The reason I don't just use the graphics view mouse event is because, even though it was working before to an extent, it broke when I scrolled because the mouse event's position doesn't scroll with it.
This doesn't work the same as that did, though, when it comes to selection of the whole item. Now I'm back where I started and am considering creating a rectangle around the point & getting the containing items of that & doing the boundingRect() logic myself. The code that somewhat worked before:
void CustomGraphicsView::mousePressEvent(QMouseEvent *evt) {
switch (evt->button()) {
case Qt::LeftButton: {
QList<QGraphicsItem *> items =
this->scene()->items(evt->pos(), Qt::IntersectsItemBoundingRect);
QListIterator<QGraphicsItem *> it(items);
while (it.hasNext()) {
QGraphicsItem *item = it.next();
if (item->boundingRegion(item->sceneTransform()).contains(evt->pos())) {
item->setSelected(!item->isSelected());
break; // This can happen twice cuz of float rounding nonsense
}
}
break;
}
case Qt::RightButton: {
scene()->clearSelection();
break;
}
default: {
break;
}
}
QGraphicsView::mousePressEvent(evt);
}