在 QGraphicsView 中禁用橡皮筋选择并且只有单个项目选择

Disable Rubberband selection in QGraphicsView and only have single item selction

如何在 QGraphicsView 中禁用 Rubberband selection 并且只允许用户在工具中单击 select 单个项目?

谢谢

如果我理解正确,你想禁用橡皮筋 selection 并且仍然可以左键单击 select 项(允许 Ctrl 修饰符以便 select 多项,一次一项)。

所以如果是这种情况,就需要使用QGraphicsView::setDragMode方法,设置QGraphicsView::NoDrag选项。您可以直接从 QGraphicsView 对象或子类化 QGraphicsView 并在构造函数上添加对方法的调用来实现此目的,例如 (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)

如果您的图形项目启用了 Qt::ItemIsSelectable 标志,那么您仍然可以像往常一样 select 它们。

希望对您有所帮助。