Search code examples
pysideqgraphicsview

Disable Rubberband selection in QGraphicsView and only have single item selction


How can i disable the Rubberband selection in QGraphiscView and only allow users to click select single items as a time in the tool?

Thanks


Solution

  • If I understand correcly, you want to disable the rubber band selection and still be able to left-click for select items (allowing the Ctrl modifier in order to select multiple items, one at the time).

    So if this is the case, you need to use the QGraphicsView::setDragMode method and set QGraphicsView::NoDrag option. You can achieve this directly from your QGraphicsView object or subclassing QGraphicsView and adding the call to method on the constructor, like this (PySide):

    from PySide.QtGui import *
    from PySide.QtCore import *
    
    
    class MyGraphicsView(QGraphicsView):
        def __init__(self, parent = None):
            super(MyGraphicsView, self).__init__(parent = parent)
            self.setDragMode(QGraphicsView.NoDrag)
    

    If your graphic items have the Qt::ItemIsSelectable flag enabled, then you will still be able to select them as usual.

    Hope it helps.