I have a QGraphicsScene where I have QGraphicsItems and enabled rubberBand selection. I want to select these items with rubberband selection but I want them to become selected only when the rubber band is released. Now it selects/deselects items live time. So, items must get selected only when I release the rubberband. I think I might need to completely change the way I add the rubber band but I don't quite know how.
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()
Also I want to make my rubberband area colored and semi-transparent. I've read the docs but can't correctly implement everything that I've read. Any help would be greatly appreciated!