PDA

View Full Version : QGraphicsScene/QGraphicsView is selecting only one item



jjkrol
28th May 2011, 17:11
Hello!

Situation:
I have a QGraphicsScene, QGraphicsView and several QGraphicsEllipseItems on it;
I enabled selecting and moving for the ellipse items;

What happens:
When I double click on an item, it is being selected - that's ok.
When I double click on another item then, it is not being selected, but the first still has its selection.

I overrode the doubleMouseClick method of QGraphicsView and I made it clear all the items from slection, before calling its parent method. It looks like this

void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *event){
QList<QGraphicsItem*> list = this->scene()->items();
foreach(QGraphicsItem* item, list){
item->setSelected(false);
qDebug()<<"selection: "<<item->isSelected();
}
QGraphicsView::mouseDoubleClickEvent(event);
foreach(QGraphicsItem* item, list){
qDebug()<<"selection: "<<item->isSelected();
}
}

It behaves now like this:
before click: one item has isSelected()=true;
after clicking, before calling parent method: all items have isSelected()=false;
after calling parent method: the first item (not the one I clicked now) has isSelected()=true;

So, as a result, the first clicked item "steals" the selection from the others.
By the way, I checked the result of itemAt(mouse position) function, and it returns the proper item.

I'm very confused, help would be really appreciated.

joyer83
28th May 2011, 21:49
According to Qt's documentation the items() method returns the items in descending stacking order. Maybe the first item (topmost) is so large that it covers the other items also?
I would rather show the selection visually in the scene than using qDebug(). It might be easier that way to see what really happens.

SixDegrees
28th May 2011, 22:27
I would rather show the selection visually in the scene than using qDebug(). It might be easier that way to see what really happens.

Modify your item's paint method to use the isSelected() setting to determine, for example, the outline color.

jjkrol
29th May 2011, 12:55
The selection is also visible, and it stays on the first item I clicked. It doesn't also depend on the stack order - whichever item I click first, it stays selected all the time.