5

QGraphicsScene在 PyQT 中进行子类化,并希望将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()
4

1 回答 1

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_())
于 2015-02-11T13:06:35.453 回答