我在PyQT中对QGraphicsScene
进行了子类化,并希望将 Shift 键(修饰符)用于多选项而不是 control 键。
我可以通过继承它并创建我自己的mousePressedEvent
来实现,但是当我这样做时,当我开始拖动它们时,场景中的graphicsItem表现得很奇怪。
我需要的是将 control 键改为 shift 键以进行多选。如果有人能提供帮助,我将不胜感激。
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class GraphicsScene(QGraphicsScene):
def __init__(self, parent=None, ns=""):
super(GraphicsScene, self).__init__(parent)
self.parent = parent
self.prevItem = []
self.ns = ns
self.setBackgroundBrush(QBrush(QColor(50,50,50) , Qt.SolidPattern))
def mouseReleaseEvent(self, event):
if event.button() == 2: #Right mouse click
#Set previous selections selected
for item in self.prevItem:
item.setSelected(1)
item = self.itemAt(event.lastScenePos ().x(), event.lastScenePos ().y())
if item:
item.setSelected(1)
if event.button() == 1: # Left mouse click
#Get selected item
items = self.selectedItems()
#Shift click
if event.modifiers() & Qt.ShiftModifier:
#Set previous items selected
for item in self.prevItem:
item.setSelected(1)
for item in items:
self.prevItem.append(item)
item = self.itemAt(event.scenePos ().x(), event.scenePos ().y())
if item == None:
self.clearSelection()
else:
self.prevItem = []
for item in items:
self.prevItem.append(item)
super(GraphicsScene, self).mouseReleaseEvent(event)
def mousePressEvent(self, event):
if event.modifiers() & Qt.ShiftModifier:
item = self.itemAt(event.scenePos ().x(), event.scenePos ().y())
if item:
item.setSelected(1)
super(GraphicsScene, self).mousePressEvent(event)
class MainClass(QMainWindow):
def __init__(self, parent=None):
super(QMainWindow, self).__init__(parent)
self.setGeometry(50,50,400,600)
widget = QWidget()
self.setCentralWidget(widget)
layout = QGridLayout()
layout.setMargin(3)
widget.setLayout(layout)
view = QGraphicsView()
view.setMouseTracking(1)
view.setDragMode(QGraphicsView.RubberBandDrag)
view.setRenderHint(QPainter.Antialiasing)
view.setRenderHint(QPainter.TextAntialiasing)
view.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
view.setAlignment(Qt.AlignJustify)
scene = GraphicsScene(self)
scene.setSceneRect(0,0, 300, 500)
view.setScene(scene)
layout.addWidget(view)
elipseOne = QGraphicsEllipseItem (30,30,30,30, parent=None, scene=scene)
elipseOne.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable)
elipseTwo = QGraphicsEllipseItem (60,60,30,30, parent=None, scene=scene)
elipseTwo.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable)
win = MainClass()
win.show()
答案 0 :(得分:1)
修改了你的mousePress和mouseRelease活动希望它有用
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from collections import deque
class GraphicsScene(QGraphicsScene):
def __init__(self, parent=None, ns=""):
super(GraphicsScene, self).__init__(parent)
self.parent = parent
self._selectedItemVec = deque()
self.ns = ns
self.setBackgroundBrush(QBrush(QColor(50,50,50) , Qt.SolidPattern))
def mouseReleaseEvent(self, event):
item = self.itemAt(event.scenePos ().x(), event.scenePos ().y())
if item:
item.setSelected(1)
else:
if len(self._selectedItemVec):
self._selectedItemVec.popleft()
return QGraphicsScene.mouseReleaseEvent(self, event)
if event.modifiers() & Qt.ShiftModifier:
for item in self._selectedItemVec:
item.setSelected(1)
else:
self._selectedItemVec.popleft()
def mousePressEvent(self, event):
item = self.itemAt(event.scenePos ().x(), event.scenePos ().y())
if item:
item.setSelected(1)
self._selectedItemVec.append(item)
else:
return QGraphicsScene.mousePressEvent(self, event)
class MainClass(QMainWindow):
def __init__(self, parent=None):
super(QMainWindow, self).__init__(parent)
self.setGeometry(50,50,400,600)
widget = QWidget()
self.setCentralWidget(widget)
layout = QGridLayout()
layout.setMargin(3)
widget.setLayout(layout)
view = QGraphicsView()
view.setMouseTracking(1)
view.setDragMode(QGraphicsView.RubberBandDrag)
view.setRenderHint(QPainter.Antialiasing)
view.setRenderHint(QPainter.TextAntialiasing)
view.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
view.setAlignment(Qt.AlignJustify)
scene = GraphicsScene(self)
scene.setSceneRect(0,0, 300, 500)
view.setScene(scene)
layout.addWidget(view)
elipseOne = QGraphicsEllipseItem (30,30,30,30, parent=None, scene=scene)
elipseOne.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable)
elipseTwo = QGraphicsEllipseItem (60,60,30,30, parent=None, scene=scene)
elipseTwo.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainClass()
win.show()
sys.exit(app.exec_())