我有一个 QGraphicsScene,我有 QGraphicsItems 并启用了 RubberBand 选择。我想通过橡皮筋选择来选择这些项目,但我希望它们仅在释放橡皮筋时才被选中。现在它可以实时选择/取消选择项目。因此,只有在我松开橡皮筋时才能选择项目。我想我可能需要完全改变我添加橡皮筋的方式,但我不太清楚怎么做。
ui_path = "C:/Users/User/ui/button_test.ui"
class Test(QtWidgets.QWidget):
def __init__(self):
super(Test, self).__init__()
loader = QtUiTools.QUiLoader()
self.ui = loader.load(ui_path, self)
self.scene = QtWidgets.QGraphicsScene()
self.ui.graphics_view.setScene(self.scene)
self.ui.create_rect.clicked.connect(self.itemAdd) # create_rect is a QPushButton
self.ui.graphics_view.setDragMode(QGraphicsView.RubberBandDrag)
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint)
def itemAdd(self, event):
pen = QPen(Qt.GlobalColor.lightGray)
pen.setWidth(10)
brush = QBrush(Qt.GlobalColor.lightGray)
rect = self.scene.addRect(0, 0, 40, 40, pen, brush)
rect.setFlag(QGraphicsItem.ItemIsMovable)
rect.setFlag(QGraphicsItem.ItemIsFocusable)
rect.setFlag(QGraphicsItem.ItemIsSelectable)
if __name__ == '__main__':
win = Test()
win.ui.show()
我也想让我的橡皮筋区域有颜色和半透明。我已阅读文档,但无法正确实现我已阅读的所有内容。任何帮助将不胜感激!