Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Disable multiple selection in QGraphicsScene



  • Hi,

    I add few QGraphicsItem in QGraphicsScene. For each item I set the flag setFlags(QGraphicsItem::ItemIsSelectable);. Now when I launch the application and pick few items while CTRL is hold then I get every pressed item is selected.
    Is there a way to disable multiple selection?


  • Lifetime Qt Champion

    Hi,

    I don't think there's an easy way to do that. One possibility would be to subclass QGraphicsView and re-implement its mousePressEvent. Create a new QMouseEvent object that is the copy of the one you are getting minus the modifier and then call the base class implementation with that new event.


  • Lifetime Qt Champion

    Hi,

    I don't think there's an easy way to do that. One possibility would be to subclass QGraphicsView and re-implement its mousePressEvent. Create a new QMouseEvent object that is the copy of the one you are getting minus the modifier and then call the base class implementation with that new event.



  • @SGaist Thank you for response
    Is there a significant difference whether to implement mousePressEvent in QGraphicsView or in QGraphicsScene class?



  • I have another similar question.
    I implemented few movable items and there is interesting things. When there is a selected item on the scene and I fast move the mouse without stop and click and hold mouse_1 on the unselecteditem then I move both items: the item I hold and an item is selected (like my computer has no time to change selected item and move the both)
    Is there a way to overcome this?

    I tried to use QThread::sleep() in MyItem::itemChange() this helps but no matter how long is sleep parameter the selected item still do a little position change (few millimeters and the it stops)
    The same result I get if I implement in in MyItem::itemChange():

    scene()->clearSelection();
    this->setSelected(true);
    

    This happens in both debug and release mode
    1.png


  • Lifetime Qt Champion

    Because the extended selection is implemented in the QGraphicsView::mousePressEvent.


  • Lifetime Qt Champion

    As for your next question, I wonder if you are not triggering the rubber band selection somehow. You can try to disable it.

    Note: it's never a good idea to put your main thread to sleep because you are going to block the event from processing events and thus make your GUI somewhat unresponsive.



  • @SGaist thank you for reply
    I didn't use rubber band selection (at least I didn't write the code with somehow similar to it)
    I decided to turn off the selection flag and do my own "selection" when the item is clicked (setPen wider).

    Sorry for answering so late.


Log in to reply