I have a QGraphicsScene (let's call it mother scene) that includes items, and some of them include a QGraphicsScene as well (let's call them daughter scenes).
Observation : The selection in the daughter scene and in the mother scene don't bother each other. It means that, if I select an item in the mother scene, I can select items in the daughter scene and the items in the mother scene will remain selected.
My expected result : I would like my selection in a scene to clear the selection in any other scene.
[Edit for clarity] When I select an item in a scene, I would like to unselect the selected items in the other scenes, not to have several focuses in different scenes.
My motivation : I want to do that because when I use a keyboard shortcut, I don't know which scene will be chosen by Qt. When I have only one daughter, the mother is chosen (I would prefer the daughter).
My solution so far : In the daughter scene container item, the mousePressEvent clears the mother scene's selection. I find this solution pretty ugly and I would like to know if someone does know a better solution that would use some inner Qt features. Now it looks like a bad DIY solution that will bring many issues.
Thanks in advance !
[Edit : minimal example] In this example, we can select the two nested elements simultaneously. I would rather have only one selection at a time in my whole scene.
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMainWindow>
#include <QGraphicsItem>
#include <QGraphicsLinearLayout>
#include <QGraphicsWidget>
#include <QGraphicsProxyWidget>
// Item that gets red contour when selected
class SimpleItem : public QGraphicsItem
{
public :
SimpleItem():QGraphicsItem()
{
setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QRectF boundingRect() const override { return QRectF(-20, -20, 40, 40);}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
{
painter->setPen(Qt::black);
if(isSelected())
painter->setPen(Qt::red);
painter->setBrush(Qt::gray);
painter->drawRect(boundingRect());
}
};
// Item that contains a QGraphicsScene in a layout
// This item gets also a red contour when selected
class SceneItem : public QGraphicsWidget
{
public :
SceneItem():QGraphicsWidget()
{
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFocusPolicy(Qt::ClickFocus);
// Create the inner scene
QGraphicsLinearLayout * layout = new QGraphicsLinearLayout;
setLayout(layout);
QGraphicsScene * scene = new QGraphicsScene;
QGraphicsView * view = new QGraphicsView(scene);
QGraphicsProxyWidget * proxy = new QGraphicsProxyWidget;
layout->addItem(proxy);
proxy->setWidget(view);
// Add a simple item
SimpleItem * simpleItem = new SimpleItem;
scene->addItem(simpleItem);
}
QRectF boundingRect() const override { return QRectF(0, 0, 100, 100);}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
{
painter->setPen(Qt::black);
if(isSelected())
painter->setPen(Qt::red);
painter->setBrush(Qt::lightGray);
painter->drawRect(boundingRect());
}
};
// Main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QGraphicsScene * scene = new QGraphicsScene;
QGraphicsView * view = new QGraphicsView(scene);
view->setDragMode(QGraphicsView::RubberBandDrag);
w.setCentralWidget(view);
SceneItem * item = new SceneItem;
scene->addItem(item);
w.show();
return a.exec();
}