使用rubber Band选择QGraphicsItem

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用rubber Band选择QGraphicsItem相关的知识,希望对你有一定的参考价值。

我试图用rubberBand选择一些与某些预定的X和Y坐标相关联的QGraphicItems。用户可以使用鼠标滚轮进行缩放,并使用鼠标中键进行平移。用户可以单击鼠标左键并绘制选择窗口,但我目前无法选择任何一个点。我找到了一个example,我正在努力适应我的,但现在它没有找到我的自定义Point对象。

我假设我和self.findChildren(QGraphicsItem)出错了,因为我使用自己的自定义QGraphicsItem,它无法找到已添加的。我曾尝试将findChildren函数与Point对象一起使用,但又回来了相同的结果。任何建议和帮助都非常感谢。

现行守则

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from math import sqrt

class LogObject(QObject):
    hovered = pyqtSignal()
    notHovered = pyqtSignal()

class Point(QGraphicsItem):

    def __init__(self, x, y,name):
        super(Point, self).__init__()
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.rectF = QRectF(0, 0, 30, 30)
        self.x=x
        self.y=y
        self.name = name
        self._brush = QBrush(Qt.black)
        self.setAcceptHoverEvents(True)
        self.log = LogObject()

    def setBrush(self, brush):
        self._brush = brush
        self.update()

    def boundingRect(self):
        return self.rectF

    def paint(self, painter=None, style=None, widget=None):
        painter.fillRect(self.rectF, self._brush)

    def itemChange(self, change, value):
        if change == self.ItemSelectedChange:
            if value:
                self._brush = QBrush(Qt.green)
            else:
                self._brush = QBrush(Qt.black)
        return QGraphicsItem.itemChange(self, change, value)

    def hoverEnterEvent(self, event):
        self.setBrush(QColor("red"))
        self.log.hovered.emit()
        QGraphicsItem.hoverMoveEvent(self, event)

    def hoverLeaveEvent(self, event):
        self.setBrush(QColor("black"))
        self.log.notHovered.emit()
        QGraphicsItem.hoverMoveEvent(self, event)

    def mousePressEvent(self, event):
        print(self.name)
        QGraphicsItem.mousePressEvent(self, event)


class Viewer(QGraphicsView):
    photoClicked = pyqtSignal(QPoint)
    rectChanged = pyqtSignal(QRect)

    def __init__(self, parent):
        super(Viewer, self).__init__(parent)
        self.rubberBand = QRubberBand(QRubberBand.Rectangle, self)
        self.setMouseTracking(True)
        self.origin = QPoint()
        self.changeRubberBand = False

        self._zoom = 0
        self._empty = True
        self._scene = QGraphicsScene(self)

        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setFrameShape(QFrame.NoFrame)
        self.area = float()
        self.setPoints()
        QTimer.singleShot(0, self.fitInView) # This is done so that it can fit into view on load
        self.viewport().setCursor(Qt.ArrowCursor)

    def setItems(self):
            self.data = {'x': [-2414943.8686, -2417160.6592, -2417160.6592, -2417856.1783, -2417054.7618, -2416009.9966, -2416012.5232, -2418160.8952, -2418160.8952, -2416012.5232, -2417094.7694, -2417094.7694], 'y': [10454269.7008,
     10454147.2672, 10454147.2672, 10453285.2456, 10452556.8132, 10453240.2808, 10455255.8752, 10455183.1912, 10455183.1912, 10455255.8752, 10456212.5959, 10456212.5959]}
            maxX = max(self.data['x'])
            minX = min(self.data['x'])
            maxY = max(self.data['y'])
            minY = min(self.data['y'])
            distance = sqrt((maxX-minX)**2+(maxY-minY)**2)

            self.area = QRectF(minX, minY, distance, distance)
            for i,x in enumerate(self.data['x']):
                x = self.data['x'][i]
                y = self.data['y'][i]
                p = Point(x,y,'Point__'+str(i))
                p.log.hovered.connect(self.hoverChange)
                p.log.notHovered.connect(self.notHoverChange)
                p.setPos(x,y)
                self._scene.addItem(p)
            self.setScene(self._scene)



    def fitInView(self, scale=True):
        rect = QRectF(self.area)
        if not rect.isNull():
            self.setSceneRect(rect)

            unity = self.transform().mapRect(QRectF(0, 0, 1, 1))
            self.scale(1 / unity.width(), 1 / unity.height())
            viewrect = self.viewport().rect()
            scenerect = self.transform().mapRect(rect)
            factor = min(viewrect.width() / scenerect.width(),
                             viewrect.height() / scenerect.height())
            self.scale(factor, factor)
            self._zoom = 0


    def setPoints(self):
        self._zoom = 0
        self.setItems()
        self.setDragMode(self.ScrollHandDrag)

    def wheelEvent(self, event):
            if event.angleDelta().y() > 0:
                factor = 1.25
                self._zoom += 1
            else:
                factor = 0.8
                self._zoom -= 1
            if self._zoom > 0:
                self.scale(factor, factor)
            elif self._zoom == 0:
                self.fitInView()
            else:
                self._zoom = 0

    def hoverChange(self):
        self.viewport().setCursor(Qt.PointingHandCursor)

    def notHoverChange(self):
        self.viewport().setCursor(Qt.ArrowCursor)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.origin = event.pos()
            self.rubberBand.setGeometry(QRect(self.origin, QSize()))
            self.rectChanged.emit(self.rubberBand.geometry())
            self.rubberBand.show()
            self.changeRubberBand = True
            return
            #QGraphicsView.mousePressEvent(self,event)
        elif event.button() == Qt.MidButton:
            self.viewport().setCursor(Qt.ClosedHandCursor)
            self.original_event = event
            handmade_event = QMouseEvent(QEvent.MouseButtonPress,QPointF(event.pos()),Qt.LeftButton,event.buttons(),Qt.KeyboardModifiers())
            QGraphicsView.mousePressEvent(self,handmade_event)

        super(Viewer, self).mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.changeRubberBand = False
            if self.rubberBand.isVisible():
                self.rubberBand.hide()
                selected = []
                rect = self.rubberBand.geometry()
                for child in self.findChildren(QGraphicsItem):
                    if rect.intersects(child.geometry()):
                        selected.append(child)
                print ('Selection Contains:
 '),
                if selected:
                    print ('  '.join(
                        'Item: %s
' % child.name for child in selected))
                else:
                    print (' Nothing
')
            QGraphicsView.mouseReleaseEvent(self,event)
        elif event.button() == Qt.MidButton:
            self.viewport().setCursor(Qt.ArrowCursor)
            handmade_event = QMouseEvent(QEvent.MouseButtonRelease,QPointF(event.pos()),Qt.LeftButton,event.buttons(),Qt.KeyboardModifiers())
            QGraphicsView.mouseReleaseEvent(self,handmade_event)
        super(Viewer, self).mouseReleaseEvent(event)


    def mouseMoveEvent(self, event):
        if self.changeRubberBand:
            self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized())
            self.rectChanged.emit(self.rubberBand.geometry())
            QGraphicsView.mouseMoveEvent(self,event)
        super(Viewer, self).mouseMoveEvent(event)

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.viewer = Viewer(self)
        self.btnLoad = QToolButton(self)
        self.btnLoad.setText('Fit Into View')
        self.btnLoad.clicked.connect(self.fitPoints)

        VBlayout = QVBoxLayout(self)
        VBlayout.addWidget(self.viewer)
        HBlayout = QHBoxLayout()
        HBlayout.setAlignment(Qt.AlignLeft)
        HBlayout.addWidget(self.btnLoad)

        VBlayout.addLayout(HBlayout)
        self.viewer.fitInView()

    def fitPoints(self):
        self.viewer.fitInView()



if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 800, 600)
    window.show()
    sys.exit(app.exec_())
答案

findChildren方法查找Viewer的QObject子级,但QGraphicsItems不是QObject,不是Viewer的子级,因此它不是正确的解决方案。

过程是使用方法mapToScene将查看器视口坐标中的QRubberBand矩形转换为场景,然后使用方法items()在场景坐标中传递矩形。

def mouseReleaseEvent(self, event):
    if event.button() == Qt.LeftButton:
        self.changeRubberBand = False
        if self.rubberBand.isVisible():
            self.rubberBand.hide()
            rect = self.rubberBand.geometry()
            rect_scene = self.mapToScene(rect).boundingRect()
            selected = self.scene().items(rect_scene)
            if selected:
                print(
                    "".join("Item: %s
" % child.name for child in selected)
                )
            else:
                print(" Nothing
")
        QGraphicsView.mouseReleaseEvent(self, event)
    # ...

另一方面,我看到你正在创建基于QGraphicsItem的Point,最简单的是它继承自QGraphicsRectItem:

class Point(QGraphicsRectItem):
    def __init__(self, x, y, name):
        super(Point, self).__init__(QRectF(0, 0, 30, 30))
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.name = name
        self.setBrush(QBrush(Qt.black))
        self.setAcceptHoverEvents(True)
        self.log = LogObject()
        self.setPos(x, y)

    def itemChange(self, change, value):
        if change == self.ItemSelectedChange:
            self.setBrush(QBrush(Qt.green) if value else QBrush(Qt.black))
        return QGraphicsItem.itemChange(self, change, value)

    def hoverEnterEvent(self, event):
        self.setBrush(QColor("red"))
        self.log.hovered.emit()
        QGraphicsItem.hoverMoveEvent(self, event)

    def hoverLeaveEvent(self, event):
        self.setBrush(QColor("black"))
        self.log.notHovered.emit()
        QGraphicsItem.hoverMoveEvent(self, event)

    def mousePressEvent(self, event):
        print(self.name)
        QGraphicsItem.mousePressEvent(self, event)

# ...
class Viewer(QGraphicsView):
    # ...
    def setItems(self):
        # ...
        for i, (x, y) in enumerate(zip(self.data["x"], self.data["y"])):
            p = Point(x, y, "Point__" + str(i))
            p.log.hovered.connect(self.hoverChange)
            p.log.notHovered.connect(self.notHoverChange)
            self._scene.addItem(p)
        self.setScene(self._scene)
# ...

以上是关于使用rubber Band选择QGraphicsItem的主要内容,如果未能解决你的问题,请参考以下文章

band3band1怎么切换

从多个表中选择并组合成单行临时表

联通4G的频段是band1还是band3?

联通4G的频段是band1还是band3

有石墨服务器问题的橡胶宝石

联通4G FDD频段是Band1还是Band3、还是Band1和Band3都是?